2

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> 
3
  • If you post your full code, it will be helpful to solving the problem. Commented Aug 9, 2012 at 13:22
  • @bla - not the full code. Only the relevant parts - we don't want to encourage "wall of code" posts :) Commented Aug 9, 2012 at 13:23
  • Solved I Just removed multiple from HTML :) Commented Aug 9, 2012 at 13:26

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Now it gives value as "null" :(
@sag - Checkout my jsFiddle example, the code works, perhaps there is some other problem with the surrounding code? Where exactly are you extracting the value of the dropdown? Does the element exist when you make the call?
I am very Confused..Whats wrong with my case then ? it shows either null or undefined...but the selected text " FLM " appears properly when form load happens
Are there any errors in your console? I am also slightly confused because this code (as shown in the jsFiddle demo) is working...
-1

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

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

3 Comments

It gives me values as "undefined" :(
Note that this is a multi selection select element. Your code will only return the value of the first selected option and not all of them. see here
@SagarDumbre: It gives undefined as no option selected here from code and probably also from browser.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.