Link to home
Start Free TrialLog in
Avatar of Christopher Schene
Christopher ScheneFlag for United States of America

asked on

What is the Javascript code to set a select index

What is the Dom code to set this select index

<SELECT onchange="someJavaScriptcode" size=1 name=Options1 index="2"><OPTION selected>Actions<OPTION>View<OPTION value=P>Print<OPTION value=F>Fax</OPTION></SELECT>

I tried

document.forms.listing.Options1.index = 2;

without success

Thanks: I need a fast answer: 300 points!
Avatar of Christopher Schene
Christopher Schene
Flag of United States of America image

ASKER

up points to 300
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gjutras
gjutras

document.forms[0].Options1.options[document.forms[0].Options1.selectedIndex].selected=false;
document.forms[0].Options1.options[newIndex].selected=true;
as gjutras suggested, but some browser are still too stupid to handle the DOM, they sometimes work with:
  document.forms[0].Options1.options[document.forms[0].Options1.selectedIndex].setAttribute('selected',false);
  document.forms[0].Options1.options[newIndex].setAttribute('selected',true);

Keep in mind that forms[0] depend on your number of forms in the page, I'd use an id= attribute for the select tag and the get this object with document.getElementById().