Developer404
Developer404

Reputation: 5982

Cannot retrieve Selected option value

I can able to get the selected option value in onchange event. But, when clicking the ok button in the same page , I can't get the value selected. Please help me..

Code is as follows:

<select  id ="userstoassign">
<option value="s1">S1</option>
<option value="s2" selected="selected">S2</option>
<option value="s3">S3</option>
</select>
<input type="button" name="ok" id="ok" value="ok"/>

$("#ok").live('click', function(){
       var username= $("#userstoassign option:selected").val();
       alert(username);

    });

Note: In onchange event, I can get the selected value. Temp fix is I'm getting that value in onchange event in a variable and will use it in onclick event. Even in onchange event I'm getting default value if I use the code "$("#userstoassign option:selected").val();"

    $('#userstoassign').live('change',function(){                    selectedUser=this.value;
alert(selectedUser);
    });

Upvotes: 0

Views: 819

Answers (3)

Developer404
Developer404

Reputation: 5982

Reason being I'm including my jsp in another jsp which is creating this issue. But, if I create select box dynamically, then I'm not getting that issue. But, stil couldn't find why this bug is occuring.

<%@ include file="Common.jsp" %>

In this common.jsp, I'm using select box, which is creating this issue. I removed select box from this jsp and creating it dynamically in the js.

Upvotes: 0

Jayendra
Jayendra

Reputation: 52809

var username= $("#userstoassign option:selected").val();

Demo - http://jsfiddle.net/qsYwr/1/

Upvotes: 1

Irishka
Irishka

Reputation: 1136

simple:

var username=  $("#userstoassign").val();

Upvotes: 1

Related Questions