Cooki3Tube Cooki3Tube - 5 months ago 10
Javascript Question

Cannot select option with Javascript

I'm trying to select an option of a Dropdownlist using Javascript. My html Looks like the following:

<select id="active-drop" name="active">
<option value="true">Aktiv</option>
<option value="false"> Inaktiv </option>
</select>


and my Javascript look like this:

$("#active-drop option[value='true']").attr("selected",true);
$("#active-drop option[value='false']").attr("selected",false);


The Problem here is, the Attribute is set just fine but the UI isn'z updated accordingly.

btw I already tried using
selectedIndex
but this yielded the same result(the data is set correctly but the UI isn't updated)

Answer

As Tushar said:

$('#active-drop [value="true"]').prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="active-drop" name="active">
    <option value="">Just to prove it</option>
    <option value="true">Aktiv</option>
    <option value="false"> Inaktiv </option> 
</select>