Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting select's selected index dynamically

Status
Not open for further replies.

iostream71

Programmer
Mar 13, 2002
229
US
how can i set a select's selected index dynamically in all browser types(ie4/ie4 through latest revisions)
example: i want "two" to be selected, using jscript

<form name=&quot;myForm&quot;>
<select name=&quot;mySelect&quot;>
<option value=&quot;one&quot; selected>1
<option value=&quot;two&quot;>2
</select>
</form>
thnx
 
document.myForm.mySelect.selectedIndex = 1;

this is core javascript and therefore browser independent.
======================================

if (!succeed) try();
-jeff
 
no, i mean like this:

if(ns4)
do something
if(ie4)
do something
if(ie5+ || ns6)
do something

mainly concerned with the ie5+/ns6 use of getElementById;
i can't get it to work

tried:
document.getElementById(&quot;mySelect&quot;).options[1].selected = true;
it worked for ie6, but not ns6

also, how can i specify the option by name, instead of index in that example( tried &quot;two&quot;, but no luck )?

 
iostream71,

if you know the form name and element name, why bother using getElementById()?

document.myForm.mySelect.selectedIndex = 1; will work for EVERY browser.

if you must use getElementById(), be sure to give your <select> tag an id attribute (id=&quot;mySelect&quot;)...this is why it isn't working in NS. IE is more forgiving, and accepts &quot;name&quot; as the &quot;id&quot;.

hope this helps.


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

if (!succeed) try();
-jeff
 
ok, i didn't know that i could just use the name for all browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top