Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Option Based on Value [duplicate]

I'm trying to figure out a way to automatically select an option, based on a passed variable, that matches only the value of that option.

JS Fiddle Link Demo

<label>
    <span>Font Type</span>
    <select id="options>
        <option value="one">One</option>
        <option value="two" selected>Two</option>
        <option value="three">Three</option>
    </select>
</label>

function loadSelect(userFont){
    $('#options').find('option[value='+userFont+']').attr('selected', 'selected');
}

loadSelect(three);
like image 285
DRB Avatar asked Jun 17 '15 16:06

DRB


1 Answers

Just set the value on the <select> tag:

$('#options').val(userFont);

$(function() {
  $("#options").val("three");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="options">
  <option value="one">One</option>
  <option value="two" selected>Two</option>
  <option value="three">Three</option>
</select>
like image 73
Dave Avatar answered Oct 20 '22 23:10

Dave