Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value in dropdown using Jquery?

Hi I am setting a value in a drop down using jquery in the following ways.

$("#dropDownItems option[value='--Select--']").attr('selected', 'true');

 $("#dropDownItems option[value='--Select--']").attr('selected', 'selected');

But none of these are working for FireFox! and is working fine for remaining browsers.

Can any one help me out?

like image 355
sreenu Avatar asked Nov 24 '11 04:11

sreenu


3 Answers

This should do the trick for you.

$('#dropdownID').val("Value to be selected");
like image 70
Josh Mein Avatar answered Oct 21 '22 21:10

Josh Mein


$("#dropDownItems").val("--Select--");

Or, if you just want to select the first option regardless of its value, you can define this helper function:

$.fn.selectFirst = function () {
    return $(this).find("option:first").attr("selected", "selected").end();
}

Then use it like this:

$("#dropDownItems").selectFirst();
like image 21
Chris Fulstow Avatar answered Oct 21 '22 21:10

Chris Fulstow


If you want to set the default value and it is the first element, you can do:

$('#dropDown')[0].selectedIndex=0;
like image 1
user872152 Avatar answered Oct 21 '22 20:10

user872152