select option for select element from data attribute using JQuery

Viewed 78

I want to select option for select element from value in select element data attribute..

<?php 
$selectval = 0;
?>

<select data-value="<?=$selectval?>">
<option value="1">yes</option>
<option value="0">no</option>
</select>

what I did in JQuery:

$('select').val($('select').data('value'));

but it is not working, I am still getting the first option as selected

I also tried to print the value directly to value attribute also not working

<?php 
$selectval = 0;
?>

<select value="<?=$selectval?>">
<option value="1">yes</option>
<option value="0">no</option>
</select>

NOTE: I have more than one select element in the DOM and I want all of them to get values from data-attribute

0 Answers
Related