[thelist] Jump to entry in select box?

Matthew Fischer mfischer at e-fishsolutions.com
Wed Nov 22 09:59:24 CST 2000


I'm not sure I understood exactly what you were asking, but the following will change the selectedIndex of your pulldown menu to the first option where the text (not the value) is the same as the value of the checkbox. The way this is written it will only change the value of the pulldown when the box is checked, not when it is unchecked. If you want it to change on checking or unchecking, remove the line that says:

   if (checkbox.checked==true) {

and the last right curly bracket.

I would think something like this would be better done with radio buttons then checkboxes, but here ya go anyways:

<SCRIPT LANGUAGE="JavaScript">
<!--

function whichOne(checkbox) {
   var getInput = checkbox.value;
   if (checkbox.checked==true) {
   for (var i =0; i < document.form1.pulldown.options.length; i++) {
       if (document.form1.pulldown.options[i].text == getInput) {
       break
       }
       document.form1.pulldown.options.selectedIndex = i+1;
   }
   
 }

}
//-->
</SCRIPT>

<form name="form1">
    <input type="checkbox" onClick="whichOne(this)" value="Sean">
    <input type="checkbox" onClick="whichOne(this)" value="Mick">
    <input type="checkbox" onClick="whichOne(this)" value="Chris">
    <select name="pulldown">
        <option value="">choose one...
        <option value="2">Mick
        <option value="37">Sean
        <option value="9">Chris
        <option value="1">Mark
    </select>
    <input type="submit">
</form>


On 11/22/00 at 11:45 AM, Mike.Hodgson at citywatch.co.uk (Mike.Hodgson) wrote:

> Hi guys,
> 
> I'm trying to fumble my way round a bit of javascript and am wondering it's
> possible to jump (using onClick in a checkbox) to a specific entry in a
> pulldown list based on the *value* of the entry.  I know I can jump to a
> specific entry in the options *array* using something like:
> 
> <form name="form1">
>   <input type="checkbox"
> onClick="document.form1.pulldown.options[2].selected=true">
>   <select name="pulldown">
>       <option value="">choose one...
>       <option value="2">Mick
>       <option value="37">Sean
>       <option value="9">Chris
>       <option value="1">Mark
>   </select>
>   <input type="submit">
> </form>
> 
> which will jump to "Sean".  Is there a way I can jump to "Chris" by
> referring to the number "9"???
> 
> I hope that made sense.
> Cheers,
> Mick
> PS: a cc to my email address would be great as I'm on the digest.




More information about the thelist mailing list