简体   繁体   中英

change selected option by a variable

<div class='titlewrap' data-status = 'public'>PUBLIC</div>
<div class='titlewrap' data-status = 'private'>PRIVATE</div>

<select id='selstatus'>
<option value = 'public'>PUBLIC</option>
<option value = 'private'>PRIVATE</option>
</select>

JS Code

I need to change selected option by value of the data status of clicked titlewrap .

Something like

$('.titlewrap').click(function(){
    var status = $(this).data('status');
    $('#selstatus').selectedOption(status);
});

Any help?

Just set the value of select using .val()

 $('.titlewrap').click(function() { var status = $(this).data('status'); $('#selstatus').val(status); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='titlewrap' data-status='public'>PUBLIC</div> <div class='titlewrap' data-status='private'>PRIVATE</div> <select id='selstatus'> <option value = 'public'>PUBLIC</option> <option value = 'private'>PRIVATE</option> </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM