select - How do I make an option tag selected by its value attribute in jQuery? -


<select id="cars" name="cars">     <option value="1">volvo</option>     <option value="2">ferrari</option>     <option value="3">mercedes</option> </select> 

i want make option value 2 (ferrari) selected. i'm kinda new jquery. tried following without success:

var modelid = 2; jquery( "#cars" ).val( jquery( "#cars option[value=" + modelid + "]" ).val() ); 

set value using val function.

$("#cars").val('2'); 

try out example.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -