Solved

select an item in a listbox by value

Posted on 2006-06-15
8
867 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
[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
  • 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 Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

717 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