0

I am trying to simplify some of the bookmarklets that I have created over the past few days and make them easier for people to use. One of my bookmarklets changes the selected option in a form select field. I would like to able to write it as:

javascript:(function(){setSelect('yourMom'); ...boringStuffThatThey'llMessUp ...})(); function setSelect(selectThis) {moreBoringStuff}

where "yourMom" is one of the select options that they can choose from on the web page. In the HTML, however, 'yourMom' has an integer value. My thought was to do something like:

function setSelect(selectThis) {yourMom=1;yourSister=2;yourGirlFriend=3; ... stuff ... {if(thisSelect.options[i].value==(?)selectThis){... moreStuff ...}}}

Which leaves me needing to know what I need to put in place of the (?)selectThis so that it points to the value stored in yourMom.

I hope that makes sense to someone out there.

Thanks for the help.

edit:

the HTML looks something like this:

<select name="whoDoILikeMost" tabindex="2" class="dropdown">
        <option value="1" selected="selected">yourMom</option>
        <option value="2">yourSister</option>
        <option value="3">yourGirlFriend</option>
    </select>
Aaron Luman
  • 635
  • 1
  • 10
  • 29

1 Answers1

0

You could iterate over each of the options in the select element and set the value if "name" element matches with the supplied value, like so:

function setSelect(name) {
   var selectEl = document.getElementById("selectElementName");
   for (var i = 0; i < selectEl.options.length; i++) {
       if (selectEl.options[i].name == name) {
           selectEl.selectedIndex = i;
           break;
       }
   }
}
Ryan Brunner
  • 14,723
  • 1
  • 36
  • 52