Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript: Drop down select box

Status
Not open for further replies.

aj2taylo

Programmer
Nov 4, 2002
17
CA
Hello,

I have a select box that I'm trying to integrate with my site. If a user clicks on a certain image, I want to then populate the select box with the appropriate corresponding value.

I'm familiar with the document.getElementById("somefield").selectedIndex = "somevalue" method, but I'm wondering if there's a way to do it based on the option value.

Thanks you
 
not enough information with a meaningful answer. Your first paragraph asks about changing the select box's options when a user clicks on an image. this is, of course, possible.

your second paragraph then mentions doing "it" based on the option value.

what is it you're trying to achieve?

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Here's some clarification

Say I have a dropdown box, where the options are dynamically created through a Perl script.

So, it generates something like:

<select name="dropdownbox" id="dropdown">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
...
<option value="optionX">Option X</option>
...
<option value="optionZ">Option Z</option>
</select>

Say the user clicks on some button that indicates they want Option X -> I'd like the javascript function to select Option X based on the value "optionX" rather than the index value.

Thanks!


 
example. helpful?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script type="text/javascript"><!--

function doSelect(s, v) {
    var o = s.options;
    for ( var i = 0; i < o.length; i++ ) {
        if ( o[i].value == v ) {
            s.selectedIndex = i;
            return;
        }
    }
}

//--></script>

</head>

<body>

<form name="f"><fieldset>
<select name="s">
    <option value="blah">Blah</option>
    <option value="foo">Foo</option>
    <option value="bar">Bar</option>
</select>
</fieldset></form>

<img src="foo.gif" style="height: 50px; width: 200px;" alt="Select 'foo'" onclick="doSelect(document.f.s,'foo');" />

</body>
</html>

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
beware of active imagination: [URL unfurl="true"]http://www.coryarthus.com/[/url]

BillyRayPreachersSonIsTheLeetestHax0rDude
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top