Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change selected item in jQuery [duplicate]

Tags:

jquery

Hi I wrote that html codes and I want change selected item by using jQuery

<select id="slc"> 
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

I tried this but I can't change

$("#slc").val("2");

$("#slc").prop("selectedIndex", 1);
like image 527
Enes Avatar asked Mar 27 '16 11:03

Enes


1 Answers

$("#slc option[value=3]").prop('selected', 'selected');//get option with value 3 and set selected
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="slc">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Do like this

like image 136
guradio Avatar answered Sep 28 '22 09:09

guradio