Solved

select an item in a listbox by value

Posted on 2006-06-15
8
865 Views
Last Modified: 2012-06-27
i have a listbox on a form and I want to automatically select (i.e. highlight) one of the items using a javascript function
i will supply the value, but I'm not sure how to select the line based on the value, not the index
in the real world, the values in the select box below will be coming from a database and wont be sequential
so i need to select by value

<form name=thisForm id=thisForm>
<select name="S1" >
<option value=1>Choice 1</option>
<option value=2>Choice 2</option>
<option value=3>Choice 3</option>
<option value=4>Choice 4</option>
</select>

<SCRIPT LANGUAGE=javascript>
     document.thisForm.S1.???????=1
</SCRIPT>

</form>
0
Comment
Question by:bigbillydotcom
  • 4
  • 4
8 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16914042
Like this:

<form name=thisForm id=thisForm>
<select name="S1" >
<option value=1>Choice 1</option>
<option value=2>Choice 2</option>
<option value=3>Choice 3</option>
<option value=4>Choice 4</option>
</select>

<SCRIPT LANGUAGE=javascript>
     document.thisForm.S1.value=1
</SCRIPT>

</form>

Freaking easy ;-)
0
 
LVL 13

Author Comment

by:bigbillydotcom
ID: 16914084
that doesnt actually select the option

i want to scroll the list and select that item by value
0
 
LVL 13

Author Comment

by:bigbillydotcom
ID: 16914090
when i say select - i want that choice highlighted
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 13

Author Comment

by:bigbillydotcom
ID: 16914094
in VB there was something like SelectByValue - but not sure how this is done in javascript
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16914126
Put this and you will see that it is selected:

     document.thisForm.S1.value=3;

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 16914137
And to see it in blue add the focus method:
<SCRIPT LANGUAGE=javascript>
     document.thisForm.S1.value=3;
     document.thisForm.S1.focus();
</SCRIPT>

0
 
LVL 13

Author Comment

by:bigbillydotcom
ID: 16914151
GOTTA LOVE THE ZVONKO
Thats it bro - GREAT!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16914172
Uhps! Keep your temper! :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
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'…
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…

810 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