Javasuck - Grin with cat attached — LiveJournal
Previous Entry Next Entry
Javasuck Dec. 5th, 2002 04:53 pm
An HTML page with two identical <SELECT> controls.
( [=< etc):

[SELECT name="select1"]
[OPTION value="3"]A[/OPTION]
[OPTION value="5"]B[/OPTION]
[OPTION value="7"]C[/OPTION]
[OPTION value="9"]D[/OPTION]

[SELECT name="select2"]
[OPTION value="3"]A[/OPTION]
[OPTION value="5"]B[/OPTION]
[OPTION value="7"]C[/OPTION]
[OPTION value="9"]D[/OPTION]

Javascript OnChange routine sets: select1.value=select2.value

Set select2 to A, (offset=0,value=3).

In IE-MAC, select1 will now be D (offset=3, value=7)
In MOZ-MAC and IE-WIN, select1 will now be A (offset=0, value=3)

This is a bloody PITA. Or am I just nuts?

You're nuts

From: emomisy
Date: December 5th, 2002 - 09:07 am (Link)
You're trying to use javascript ;-)

Not as nuts as the boss I had who wanted me to write cryptography routines in javascript though... I ran away...

Re: You're nuts

From: wechsler
Date: December 5th, 2002 - 09:38 am (Link)
Wel yes. But The Client Wants It.
From: dennyd
Date: December 5th, 2002 - 09:24 am (Link)
So IE is using the value as an offset instead of as a, erm, value.  Cute  :)

What happens if the values are non-numeric in form?  Words, or something along those lines...?
From: djm4
Date: December 5th, 2002 - 09:27 am (Link)
You're not nuts, it's just not possibly to reliably set the value of a select object. In all my bits of code that do it, I've got a small routine that iterates through the options array of the select object, finds the one whose value matches, and sets select.selectedIndex to that index.

Um... like this:

function setDropDown(formField, value) {
	if (!formField) return;
	if (formField.type != 'select-one' && formField.type != 'select-multiple') return;
	for (var iLoop = 0; iLoop < formField.length; iLoop++) {
		if (formField.options[iLoop].value) {
			if (formField.options[iLoop].value == value) { formField.selectedIndex = iLoop; }
		} else if (formField.options[iLoop].text) {
			if (formField.options[iLoop].text == value) { formField.selectedIndex = iLoop; }

From: wechsler
Date: December 5th, 2002 - 09:41 am (Link)
Hrm. Since the lists are identical maybe I should just do it by selected index.
From: djm4
Date: December 5th, 2002 - 09:53 am (Link)
In all versions of JavaScript I've worked with (which admittedly isn't that many and not at the bleeding edge of JavaScript development), the value property of a Select object has been considered meaningless. Each of the Option objects in a Select object has a value (which you can, indeed, set), but the Select object itself doesn't.

Inconvenient, certainly, but I can understand why it's like that.
From: wechsler
Date: December 5th, 2002 - 09:59 am (Link)
Well, the thing is that it works in Moz and on Windows, as I'd expected, and the .value property is equal to the value= attribute of the currently selected option.

If the .value property (or setting it, at least) were supposed to have no purpose, it shouldn't be possible to set it.

Still, I'll sort this out tomorrow.
(no subject) - (Anonymous)
From: wechsler
Date: December 5th, 2002 - 11:05 am (Link)
Yes, I know... logic and computers, bad combination.