I have this select
<select id="myselect>
<option value="1" data-value="rome">First option</option>
<option value="2" data-value="paris">Second option</option>
<option value="3" data-value="london">Third option</option>
</select>
And I have my ajax function that on success must set selected by data-value
$.ajax({
...
success: function(response) {
// response.val is paris
$("#myselect option[data-value=" + response.val +"]").attr("selected","selected");
...
But doesn't work because of "unrecognized expression"
Your id "myselect
is missing the closing ", it should be
<select id="myselect">
..
</select>
Use
$("#myselect option[data-value='" + response.val +"']").attr("selected","selected");
Here's a fiddle:
https://jsfiddle.net/k38efh2o/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With