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

How to set value of drop-down-menu with JavaScript?

P: 31
Hi,

I got a drop-down-menu with dynamically parameters. I have set a specific value in my JavaScript and I would like to change the shown value in the drop-down-menu to the value of my JavaScript when first open the html-file.

So I do not want to make a new entry in my drop-down-menu as all needed entries are already available due to the dynamic drop-down-menu.

What I wanna do is to compare my desired value in my JavaScript with the values of my drop-down-menu and if there is something identical then change the value of the drow-down-menu to that desired value.

What I don't understand to solve that problem is:
1. How can I read all the entries of a drop-down-menu and compare it with my value in JavaScript?
2. How can I select one specific entry when I open the html-file?
4 Weeks Ago #1

✓ answered by gits

looping could look like this:

Expand|Select|Wrap|Line Numbers
  1. var mySelectNode = document.getElementById("mySelect");
  2.  
  3. for (var i = 0, l = mySelectNode.options.length; i < l; i++) {
  4.  
  5.     var opt = mySelectNode[i];
  6.     console.log(opt.text);
  7. }

Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,283
what have you tried so far? here are the ideas behind it:

1: get a reference to the select-element that you want to test with and loop over the options to find out a match

2: get a reference to the select-element where you want to set the value and set the selected attribute or use a setter for the value that the element provides
4 Weeks Ago #2

P: 31
So what I have in my xslt:
Expand|Select|Wrap|Line Numbers
  1. <th rowspan="2">plane
  2.                 <select id="modelRangeDropdown" onclick="JavaScript_Filter(this)">
  3.                         <option selected="selected">All</option>
  4.                         <xsl:for-each select="logstore/plane">
  5.                         <option>
  6.                             <xsl:value-of select="Name" />
  7.                           </option>
  8.                      </xsl:for-each>                    
  9.                 </select>                    
  10.             </th>    
  11.  
And what I was trying to do was something like:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("modelRangeDropdown")[0].text;
  2. ... // But how to loop over the options?
  3.  
4 Weeks Ago #3

gits
Expert Mod 5K+
P: 5,283
looping could look like this:

Expand|Select|Wrap|Line Numbers
  1. var mySelectNode = document.getElementById("mySelect");
  2.  
  3. for (var i = 0, l = mySelectNode.options.length; i < l; i++) {
  4.  
  5.     var opt = mySelectNode[i];
  6.     console.log(opt.text);
  7. }
4 Weeks Ago #4

P: 31
Thanks for you answer. That's great. Does there exist a function for selecting an entry of a drop-down-menu for JavaScript like selecting it with a computer mouse?
4 Weeks Ago #5

P: 31
I have found the solution:
Just use:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("modelRangeDropdown").selectedIndex = row_number_of_dropdown_value;
  2.  
4 Weeks Ago #6

Post your reply

Sign in to post your reply or Sign up for a free account.