tags:

views:

312

answers:

3

hi, traditionally we write

var value = $("#drop-down-id").val();

to get the value. IN my case, some of the options contains multiple words with spaces like "Allow All","Internet Users" etc. What I am getting in VALUE is only "Allow" that is, jquery is trimming off words after the white space.. Is there any other way to solve this?

+4  A: 

try with:

var value = $("#drop-down-id option:selected").attr("value");
mck89
Thanks for the reply,but it did not work.
uday
+4  A: 

If you have the value wrapped in the <option></option> and you don't use the val attribute directly, you can do:

var value = $('#drop-down-id option:selected').text();
reko_t
Thanks for the reply, your solution worked :)
uday
Might have worked for you, but please know the difference. the text() attribute gets what the DropDownList currently displays, whereas the val() attribute is the "behind" value. <option value="value">text</option>
Anthony M. Powers
Thats a very good point.. I checked in firebug, the "value" had been trimmed to one word. So, the .text was working and not the .value().Basically i m populating it dynamically from a json object. Will have to check it out. Thanks anyway!
uday
A: 

This makes no sense. There should not be any need to get the option text. If you need to do so, then your option elements are likely populated the wrong way in the server side.

Check the HTML source, you're likely forgotten the quotes around the option's value attribute.

This one is wrong:

<option value=John Doe>John Doe</option>

It would only return the first word as the actually selected value, because the space is actually an attribute separator in HTML; now you'll get "John" as value and the "Doe" is seen as another (non-existing ) HTML attribute.

This one is correct

<option value="John Doe">John Doe</option>

This way you'll get "John Doe" as value.

BalusC