Combo box onChange not working in Firefox

A

atulvp

Hi all,
I have written a javascript code which changes the combo_2 options on
runtime. It is working fine on IE but not working on Firefox. Please
help me solve this problem.

My code is as follows:

function changeMe(str) {
if(str.value == "se") {
while(document.frm.combo2.length > 0) {
document.frm.combo2.remove(0);
}
var y;
y = document.createElement('option'); y.text='ONE-1';
y.value="one-1";
document.frm.combo2.add(y, 0);

y=document.createElement('option'); y.text='one-2'
document.frm.combo2.add(y, 0);
}
}


<form name="frm">
<select id="combo1" onChange="changeMe(this)">
<option value="se">Sony Erricson</option>
<option value="c2">C2</option>
</select>

<select name="combo2">
<option value="b1">B1</option>
<option value="b2">B2</option>
</select>
</form>

Kindly suggest me what am i doing wrong?

Thanks
 
S

scripts.contact

Hi all,
I have written a javascript code which changes the combo_2 options on
runtime. It is working fine on IE but not working on Firefox. Please
help me solve this problem.

My code is as follows: ....
document.frm.combo2.add(y, 0);
document.frm.combo2.add(y, 0);
....

0 is not valid for 2nd parameter in Firefox. Pass a reference to an
option element in 2nd element e.g.
document.frm.combo2.add(y,document.frm.combo2.options[0])
 
A

atulvp

On Apr 24, 2:54 pm, "scripts.contact" <[email protected]>
wrote:
[...]
0 is not valid for 2nd parameter in Firefox. Pass a reference to an
option element in 2nd element e.g.
document.frm.combo2.add(y,document.frm.combo2.options[0])

Thanks this works but not working on IE. Any suggetion to work on all
browsers.

Regards
 
S

scripts.contact

0 is not valid for 2nd parameter in Firefox. Pass a reference to an
option element in 2nd element e.g.
document.frm.combo2.add(y,document.frm.combo2.options[0])

Thanks this works but not working on IE. Any suggetion to work on all
browsers.

try:

function changeMe(str) {
if(str.value == "se") {
var Combo2=document.frm.combo2,y
while(Combo2.options.length)
Combo2.remove(0);

y = document.createElement('option');
Combo2.appendChild(y)
y.text='ONE-1';y.value="one-1";

y=document.createElement('option');
Combo2.appendChild(y);
y.text='one-2';
}
}
 
A

atulvp

On Apr 24, 4:18 am, (e-mail address removed) wrote:
[...]

y = document.createElement('option');
Combo2.appendChild(y)
y.text='ONE-1';y.value="one-1";

y=document.createElement('option');
Combo2.appendChild(y);
y.text='one-2';
}

}

Thanks for Help
 
R

RobG

Hi all,
I have written a javascript code which changes the combo_2 options on
runtime. It is working fine on IE but not working on Firefox. Please
help me solve this problem.

My code is as follows:

function changeMe(str) {
if(str.value == "se") {
while(document.frm.combo2.length > 0) {
document.frm.combo2.remove(0);
}

To remove all the options, set the length of the options property to
zero:

document.frm.combo2.length = 0;

var y;
y = document.createElement('option'); y.text='ONE-1';
y.value="one-1";

You can declare and initialise y in one go. Also, using createElement
is somewhat buggy in IE, so use:

var y = new Option('ONE-1', 'one-1');

or

var y = document.createElement('option');
y.value="one-1";
y.appendChild(document.createTextNode('ONE-1'));

but the first method seems so much simpler.

document.frm.combo2.add(y, 0);

y=document.createElement('option'); y.text='one-2'
document.frm.combo2.add(y, 0);
}

}

<form name="frm">
<select id="combo1" onChange="changeMe(this)">

Using onchange is likely not a good idea. Try it in IE and use the
cursor keys to move down the options, as would someone using the
keyboard rather than a mouse. Compare that to what happens in Firefox
doing the same thing.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top