Index of selection is wrong after setting a new index within Mozilla

S

Stefan Mueller

I choose 'Entry 4' and click then on the button 'Set' to set the index to
'Entry 2'. If you press now the cursor down key 'Entry 5' instead of 'Entry
3' shows up with Mozilla Firefox. With Internet Explorer and Opera it works
fine; 'Entry 3' shows up.

Is this a bug within Mozilla Firefox or do I have any possibility to change
this wrong behavior?
Stefan

========================

<html>
<body>
<form name = "MyForm" action = "" method = "post">
<select name = "MySelection" size = "1">
<option value = "1">Entry 1
<option value = "2">Entry 2
<option value = "3">Entry 3
<option value = "4">Entry 4
<option value = "5">Entry 5
<option value = "6">Entry 6
</select>

<input type = "button" value = "Set" onClick =
"document.MyForm.MySelection.selectedIndex = 1;
document.MyForm.MySelection.focus();">
</form>
</body>
</html>
 
M

Matt - EditMe.com

Stefan,

I'm afraid I can't offer any solution, but can say that the problem
happens in my Firefox installation as well (1.0.7). I'd see if it's
fixed in the latest version of Firefox and if not submit a bug report
(if hasn't already been submitted).

If you wanted to work-around it, all I can think of is to keep your own
selected state in a variable, capture the key events and over-ride
Firefox's handling of the event - although that seems like more trouble
than its worth.

Cheers,
Matt
 
R

RobG

Stefan said:
I choose 'Entry 4' and click then on the button 'Set' to set the index to
'Entry 2'. If you press now the cursor down key 'Entry 5' instead of 'Entry
3' shows up with Mozilla Firefox. With Internet Explorer and Opera it works
fine; 'Entry 3' shows up.

Is this a bug within Mozilla Firefox or do I have any possibility to change
this wrong behavior?

No, it's not a bug.

The selectedIndex property of a select element is a number, not a
method, though some browsers may allow you to use it as such. To change
the selected option to index 1, use:

document.MyForm.MySelection[1].selected = true;


<URL: http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html >
and
<URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-85676760 >

[...]
 
T

Thomas 'PointedEars' Lahn

RobG said:
No, it's not a bug.

I think it is.
The selectedIndex property of a select element is a number, not a
method, though some browsers may allow you to use it as such.

Pardon? He assigned the index number 1 to that property. However, he
decribes that if the next option is selected manually via the keyboard,
it is as if 'Entry 4' (index number 3) was still selected. That _is_
a bug. It can best be observed when using Alt+Cursor Down first to
expand the list: the selection "jumps" down over two other options.

Same with other previously selected options and Cursor Up key here
in Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922
Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0
To change
the selected option to index 1, use:

document.MyForm.MySelection[1].selected = true;

That works only because it is not a multiple select. selectedIndex is
indeed the read/write property of choice for such elements. You should
read the references you are using :)


PointedEars
 
S

Stefan Mueller

To change the selected option to index 1, use:
document.MyForm.MySelection[1].selected = true;

Also with you suggestion my Mozilla Firefox experiences the same bug.
Stefan
 
R

RobG

Thomas said:
RobG wrote:



I think it is.


Pardon? He assigned the index number 1 to that property. However, he
decribes that if the next option is selected manually via the keyboard,
it is as if 'Entry 4' (index number 3) was still selected. That _is_
a bug. It can best be observed when using Alt+Cursor Down first to
expand the list: the selection "jumps" down over two other options.


I went over the whole thing again and I was completely off track, I
missed the point completely. You are exactly right, the arrow keys
don't select the next option, they go to the next option of whatever was
previously selected. You can change the selectedIndex programmatically
all you like, the previous UI-selected option is used for the next
keyboard-initiated selection.

And my 'fix' doesn't change that - yes, I'd call it a bug.


[...]
indeed the read/write property of choice for such elements. You should
read the references you are using :)

That is a good point - what are the references?

Let me go off-topic here a bit. The DOM 2 spec just says it's a number
and only mentions returned values - should I infer that any property
that isn't read only can be set? Where does it tell me what will happen
if I set the value to something?

The Mozilla Gecko DOM reference doesn't mention it - the form interface
stuff just talks about the form element.

The Netscape DevEdge stuff from 2000 on the Mozilla site which says what
you describe - the selectedIndex property should be used to set the
selected index property for single selects and for multiple selects
where previous selections should be cleared at the same time.
Apparently it was introduced in JavaScript 1.0.

<URL:
http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/select.html#1193420
The Microsoft documentation also mentions that it can be set.
 
V

vdP

Matt said:
Stefan,

I'm afraid I can't offer any solution, but can say that the problem
happens in my Firefox installation as well (1.0.7). I'd see if it's
fixed in the latest version of Firefox and if not submit a bug report
(if hasn't already been submitted).

It works fine in my FireFox 1.5.

vdP
 
T

Thomas 'PointedEars' Lahn

RobG said:
Thomas said:
[selectedIndex is] indeed the read/write property of choice for
such elements. You should read the references you are using :)

That is a good point - what are the references?

I meant the section of DOM Level 2 HTML you referred us to :)
Let me go off-topic here a bit. The DOM 2 spec just says it's a number
and only mentions returned values - should I infer that any property
that isn't read only can be set?
Yes.

Where does it tell me what will happen if I set the value to something?

It does not always. It does not in this case.
The Mozilla Gecko DOM reference doesn't mention it - the form interface
stuff just talks about the form element.

The Gecko DOM Reference is not quite complete. You can refer to
XULPlanet for all missing documentation:

<URL:http://xulplanet.com/references/objref/HTMLSelectElement.html>

The Netscape DevEdge stuff from 2000 on the Mozilla site which says what
you describe - the selectedIndex property should be used to set the
selected index property for single selects and for multiple selects
where previous selections should be cleared at the same time.
Apparently it was introduced in JavaScript 1.0. [...]

Exactly. Before JavaScript 1.4, host objects were language features.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top