Set the default value in dropdownlist using jQuery
276,208
Solution 1
if your wanting to use jQuery for this, try the following code.
$('select option[value="1"]').attr("selected",true);
Updated:
Following a comment from Vivek, correctly pointed out steven spielberg wanted to select the option via its Text value.
Here below is the updated code.
$('select option:contains("it\'s me")').prop('selected',true);
You need to use the :contains(text)
selector to find via the containing text.
Also jQuery prop offeres better support for Internet Explorer when getting and setting attributes.
Solution 2
You can just do this:
$('#myCombobox').val(1)
Solution 3
val() should handle both cases
<option value="1">it's me</option>
$('select').val('1'); // selects "it's me"
$('select').val("it's me"); // also selects "it's me"
Solution 4
$('#userZipFiles option').prop('selected', function() {
return this.defaultSelected;
});
Solution 5
$("#dropdownList option[text='it\'s me']").attr("selected","selected");
Related videos on Youtube
Author by
João Pimentel Ferreira
Updated on June 11, 2020Comments
-
João Pimentel Ferreira almost 4 years
I have many options in my dropdownlist like:
<option value="1">it's me</option>
I need to select the option who have value it's me inside the tag, not by attribute like
1
.How can I do this using jQuery?
-
Admin over 13 yearsWell i want to select them through the 'it'sme' value. but very few of us try to do with it.but thanks to all !
-
ashish.chotalia over 13 years$("#myCombobox option[text='it\'s me']").attr("selected","selected");
-
Clyde about 11 yearsstackoverflow.com/questions/292615/… Try this one..
-
-
jamesmortensen over 13 yearsWon't the use of double quotes inside double quotes create problems?
-
jamesmortensen over 13 yearsselected="selected" is also valid, and is what is suggested on W3Schools. +1
-
jamesmortensen over 13 yearsWon't you have problems with the quotes within quotes? How will JQuery know what's going on if your inner double quotes are not escaped? Perhaps I'm missing something here?
-
karim79 over 13 years
$('select').val('something')
all that is required. -
Vivek over 13 years@halfcube-according to the question steven spielberg wants to select those options which text is 'it's me' not value
-
Mark Robinson over 13 yearsThis answer is copied from stackoverflow.com/questions/496052/… and does not answer what Steven is asking.
-
Mark Robinson over 13 years@jmort253 - if you are consistent you can mix single and double quotes as long as you use one to mark the outer string and the other to use internally in the string. Eg. "This is valid ''' :) " and "This is invalid """ :(".
-
Mark Robinson over 13 yearsThe single quote in it's me needs escaping.
-
Simer Twilio Toronto developer over 13 yearsMark, $('select').val('Two'); will solve the problem.. what else Steven has asked?
-
Hogsmill over 13 yearsNot sure I can think of a situation you'd want to set all selects to a particular value? :-) Except '0', maybe...
-
Harish over 13 yearsyou are breaking the code dude check $('select option[text="it's me"]')
-
Simer Twilio Toronto developer over 13 years@vivek he wrote i need to select the option not options
-
Franco over 8 yearsThis is the simplest way to get the default options of a selectbox
-
computercarguy about 5 yearsI wasn't able to do this from the Chrome Console, but it works in the JavaScript file.
-
Panama Jack over 2 yearsStill the best answer in 2022.