tags:

views:

85

answers:

1

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>
A: 

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
I seriously doubt that the question submitter is using an ECMAScript 6 engine. __`int`__ is a reserved word.
Eli Grey
Ah, haha, I hadn't thought of using something other than the value attribute. Thanks for the help
baiano
That said, I would still like to know how to use the var varA='varB'; varB=1 so that varA can point to the value 1, or hold that value, or whatever its called.
baiano
Heh, typo on the var vs. int. Fixed now.
Ryan Brunner