Solved

Set selected index in drop down select

Posted on 2004-04-01
7
5,939 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

786 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