Solved

select an item in a listbox by value

Posted on 2006-06-15
8
863 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
Comment Utility
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
Comment Utility
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
Comment Utility
when i say select - i want that choice highlighted
0
 
LVL 13

Author Comment

by:bigbillydotcom
Comment Utility
in VB there was something like SelectByValue - but not sure how this is done in javascript
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
GOTTA LOVE THE ZVONKO
Thats it bro - GREAT!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Uhps! Keep your temper! :)
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

Suggested Solutions

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

763 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

10 Experts available now in Live!

Get 1:1 Help Now