By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

Changing selected element in a listbox

P: n/a
I have a form called myform with a listbox called combounit. I have
another listbox on the form called combolevel. When I change value in
combolevel I call a javascript function that does various things. One
thing gives me problems. How do I make it change the element selected
in combounit?

I have tried:

document.myform.combounit.options[0].selected = true;
and
document.myform.combounit.selectedIndex = 0;

but none of them work.

Regards,

Jan Nordgreen

Jun 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
document.myform.combounit.selectedIndex = 0;
this should work

damezumari написа:
I have a form called myform with a listbox called combounit. I have
another listbox on the form called combolevel. When I change value in
combolevel I call a javascript function that does various things. One
thing gives me problems. How do I make it change the element selected
in combounit?

I have tried:

document.myform.combounit.options[0].selected = true;
and
document.myform.combounit.selectedIndex = 0;

but none of them work.

Regards,

Jan Nordgreen


Jun 21 '06 #2

P: n/a
I have a form called myform with a listbox called combounit. How do I make
it change the element selected
in combounit?

I have tried:

document.myform.combounit.options[0].selected = true;
and
document.myform.combounit.selectedIndex = 0;

but none of them work.

Regards,

Jan Nordgreen


Try the following:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Select Animation</title>
<script type="text/javascript">
//<![CDATA[
<!--
var s;
var i = 0;
function test(){
i++;
if (i >= 7) i = 0;
s.selectedIndex = i;

}
window.onload = function(){
s = document.getElementById("myselect");
window.setInterval("test()", 500);
}
//-->
//]]>
</script>
</head>
<body>

<select size="7" id="myselect">
<option value="1" selected>option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
<option value="4">option4</option>
<option value="5">option5</option>
<option value="6">option6</option>
<option value="7">option7</option>
</select>

</body>
</html>
Jun 21 '06 #3

P: n/a
Thank you chonny and Marc!

It did not work for me because I used

<select size="7" name="myselect">

instead of

<select size="7" id="myselect">

Marc, I tried out your code. Very convincing! :)

Regards,

Jan Nordgreen

Jun 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.