Tuesday, January 31, 2012

jQuery select first option in a select element

Code:
$("#elementID").val($("#elementID option:first").val());
OR
$(".elementClass").val($(".elementClass option:first").val());

Description:
So basically the code above is setting the value of element ID or Class to its first option value.
You can also also find for other solutions thru web depending on your requirements.

:)

1 comment:

  1. Was looking for way to get value of first option in select and this might help.

    $("#elementID").first().val();

    OR

    $("select#elementID").first().val();

    ReplyDelete