Solved

Set selected index in drop down select

Posted on 2004-04-01
7
5,920 Views
Last Modified: 2008-02-01
Hi

I have got a dropdown select (of countries).  

As part of my page generation, I want to set the selected index of a dropdown via my javascript.

So, say in my dropdown list (of EVERY country) I wanted to select the United Kingdom, its value would be set to "UK" - here is a quick example.

<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>

So in my Javascript, i want to tell it to select "UK".   But not index number [1] if you understand what I mean, I want to use the actual value.

Many thanks

Daz
0
Comment
Question by:daz_oldham
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 250 total points
ID: 10734116
<script language=javascript>
  function setSelectIndex(sel, val) {
     for (i=0;i<sel.options.length;i++) {
        if (sel.options[i].value == val) {
           sel.selectedIndex = i;
           break;
        }
     }
  }
</script>

<body onload="setSelectIndex(document.Form1.dropdown, 'UK');">
0
 

Author Comment

by:daz_oldham
ID: 10734143
Hey bobbit, thanks for that :)

is there no way to automatically select that value?

I should imagine if the list of countries is about 30 or so, maybe even more - then that is going to take a long time to execute on the clients side?

Thanks

Daz
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 10734171
it won't take long... 30 is nothing.

BUT, if you want to do it another way:

<body onload="document.Form1.dropdown.value = 'UK';">

though i'm not sure that it is supported by all browsers

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Expert Comment

by:jaysolomon
ID: 10734482
<body onload="document.forms[0].dropdown.selectedIndex = 1">
<form name="form1" method="post" action="">
<select name="dropdown">
<option value="CH">China</option>
<option value="UK">United Kingdom</option>
</select>
</form>
</body>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 10734547
or just use the SELECTED keyword in the option:

<option value="UK" SELECTED>United Kingdom</option>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10734613
true
0
 

Author Comment

by:daz_oldham
ID: 10740180
Thanks guys for the help
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now