473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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?
Jun 24 '19 #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. }

5 1044
gits
5,390 Expert Mod 4TB
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
Jun 24 '19 #2
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.  
Jun 24 '19 #3
gits
5,390 Expert Mod 4TB
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. }
Jun 24 '19 #4
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?
Jun 24 '19 #5
I have found the solution:
Just use:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("modelRangeDropdown").selectedIndex = row_number_of_dropdown_value;
  2.  
Jun 24 '19 #6

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

Similar topics

2
by: Xerxes | last post by:
Hi, how can I pass the returned value from Javascript to PHP? I have: ------------------------------------------------------------------------ ------ if ( x>y) {
5
by: Picco | last post by:
How can i set a asp variable with a value in Javascript? JS Code: <script language="JavaScript" type="text/javascript"> <!-- if (acrobat.ver5) { // if Acrobat 5.0 or newer is installed,...
6
by: =?Utf-8?B?Um9i?= | last post by:
Hi, If I call my own DLL from Javascript, a HRESULT is returned. Javascript cannot cast this to an integer. Is there any other way of typecasting this returnvalue to integer, wihout using the...
1
by: DannyMc | last post by:
Hi, I am writing a PHP script to get a ping reply from my LAN PC. So, my idea is like this, when i mouseover/mouse click to the "hostname" display on the web. There will be a hint box appear and...
1
by: prathna | last post by:
Hi all, I would like to know how to pass a form bean value to javascript function to perform some validation.. I have a dropdown list and a textfield. I have created the dropdown list using...
17
by: akshay01 | last post by:
Hi all, I am retrieving some rows from DataBase using for loop in jsp and displaying it in a table format.In the output table I am also adding one more text box and checkbok with each row. In text...
8
by: BigZero | last post by:
Hello, i need to pass some variables or value to javascript from php script. well all i need to pass to value to javascript that is with in same php file the two values r in php i need to pass...
1
by: ghjk | last post by:
I want to take the drop down selected value using javascript. This is dropdown code <select id="user"> <option >Admin</option> <option >User</option> <option >Guest</option> </select>
16
by: BaseballGraphs | last post by:
Hello, I am trying to filter an HTML table by div value using javascript. Here is my table in HTML: <table> <tr> <td><div align="center">1</div></td> <td><div align="left"...
1
by: NEWBIEPRO | last post by:
/*This is time counter -it will update every minute I want to get the value from javascript and save it to database? */ <form name="myForm" method="post"> <script type="text/javascript"> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.