Reputation: 2293
I'm using ajax on success im getting this result.so what i need is i need to get the first value 22
how can i do that in jquery ?
<option value=22>Bed</option>
<option value=23>Bath</option>
<option value=24>Kitchen</option>
<option value=25>Living</option>
<option value=26>Foam</option>
What i have tried is
$j.ajax({
type: "POST",
url : "{{ URL::to('getcategoryname') }}",
data : {'GroupID':GroupID},
success : function(data){
console.log(data.split('option').val()); //my tried line
}
});
Upvotes: 3
Views: 2984
Reputation: 437
The below code will get you the val of the selectedIndex
$("#selectId ").prop("selectedIndex", 0).val();
Upvotes: 0
Reputation: 133403
Assuming you are getting the partial data in string format.
You can create a valid HTML then use various traversal method.
//Create select
var select = $j('<select />', {
html : data
});
//find first child option then fetch its value
var v = select.children('option:first').val(); //Even select.children('option').val(); will work
console.log(v);
Upvotes: 4
Reputation: 1554
Yes you can get first value from the following way.
var result = $('#selectId option[value!=""]').first().html();
Upvotes: 1
Reputation: 74738
I guess you need to make a jQuery object wrapper and you don't need to use split here:
console.log($j(data).find('option:first').val()); //my tried line
Upvotes: 0