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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.