Solved

Set selected index in drop down select

Posted on 2004-04-01
7
5,951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

749 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