Shaggy
Shaggy

Reputation: 5790

get Value of Selected drop down Item using jQuery

By default I have set value of drop down to 1.But when I want to retrieve that value in click event I get error. Value of that item I get it as "undefined"

$("#cmbTicketType option[value='1']").prop("selected", true);

I tried this

var sTicketType = $('#cmbTicketType').val();
alert(sTicketType );

HTML:

    <select id="cmbTicketType" name="cmbTicketType" multiple="multiple" style="display: none; ">                             
<option value="1">FLM</option>
<option value="7">Bank</option>
<option value="5">Electrical</option>
<option value="3">Network</option>
<option value="6">Power Failure</option>
<option value="2">SLM</option>
<option value="8">Suspect</option>
<option value="4">UPS</option>
                            </select>

Upvotes: 2

Views: 1589

Answers (2)

Kapil Sharma
Kapil Sharma

Reputation: 10417

Question little unclear. Assuming #cmbTicketType is the id of select tag, you ccan get value by

var sTicketType = $('#cmbTicketType option:selected').val();

Upvotes: -1

Lix
Lix

Reputation: 47956

You can set the "selected" value of a <select> element the same way you extract it's selected value.

To set the value -

$("#cmbTicketType").val(1);

Then you can retrieve the value the same way you are doing now -

var sTicketType = $("#cmbTicketType").val();

Here is a simple jsFiddle demo

Upvotes: 4

Related Questions