• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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!
0
Christopher Schene
Asked:
Christopher Schene
1 Solution
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
up points to 300
0
 
amit_gCommented:
document.forms[0].Options1.selectedIndex = 2;
0
 
gjutrasCommented:
document.forms[0].Options1.options[document.forms[0].Options1.selectedIndex].selected=false;
document.forms[0].Options1.options[newIndex].selected=true;
0
 
ahoffmannCommented:
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().
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now