Jump to content

Selected Value from a Drop Down Box


lilman

Recommended Posts

I have this code:

 

<select size="1" name="sale" onChange="actionOrSelective();">
        <option>Select</option>
        <option>Auction</option>
        <option>Selective</option>
</select>

 

Now I am trying to figure out how I can identify the value they selected.  So here is my other code:

 

var object = document.getElementById('sale').selected;

 

That doesn't work.  I was wondering if someone would help me out.  Thank you

Link to comment
https://forums.phpfreaks.com/topic/53670-selected-value-from-a-drop-down-box/
Share on other sites

Hi

 

i'm no java-pro but i think you might find it a good idea to use the value tag in the options, so

 

<select size="1" name="sale" onChange="actionOrSelective();">
        <option value="">Select</option>
        <option value="auction">Auction</option>
        <option value="selective">Selective</option>
</select>

 

and then use either your way or mybe lilman's way of getting the value??

 

cheers,

tdw

The comment above is accurate as far as needing the values within the options themselves. Otherwise, you'll need to access the elements with innerHTML, but that's another issue ;) ... To figure out which one is selected, you need to use selectedIndex of the select box:

 

function actionOrSelective() {
  var ele = document.getElementById('sale');
  var obj = ele.options[ele.selectedIndex];

  alert(obj.value);
  alert(obj.innerHTML);
}

 

Have fun with that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.