Solved

select an item in a listbox by value

Posted on 2006-06-15
8
866 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
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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

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…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

733 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