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.

A working example on JSFiddle

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"); 
Share:
276,208

Related videos on Youtube

João Pimentel Ferreira
Author by

João Pimentel Ferreira

Updated on June 11, 2020

Comments

  • João Pimentel Ferreira
    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
      Admin over 13 years
      Well 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
      ashish.chotalia over 13 years
      $("#myCombobox option[text='it\'s me']").attr("selected","selected");
    • Clyde
      Clyde about 11 years
  • jamesmortensen
    jamesmortensen over 13 years
    Won't the use of double quotes inside double quotes create problems?
  • jamesmortensen
    jamesmortensen over 13 years
    selected="selected" is also valid, and is what is suggested on W3Schools. +1
  • jamesmortensen
    jamesmortensen over 13 years
    Won'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
    karim79 over 13 years
    $('select').val('something') all that is required.
  • Vivek
    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
    Mark Robinson over 13 years
    This answer is copied from stackoverflow.com/questions/496052/… and does not answer what Steven is asking.
  • Mark Robinson
    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
    Mark Robinson over 13 years
    The single quote in it's me needs escaping.
  • Simer Twilio Toronto developer
    Simer Twilio Toronto developer over 13 years
    Mark, $('select').val('Two'); will solve the problem.. what else Steven has asked?
  • Hogsmill
    Hogsmill over 13 years
    Not sure I can think of a situation you'd want to set all selects to a particular value? :-) Except '0', maybe...
  • Harish
    Harish over 13 years
    you are breaking the code dude check $('select option[text="it's me"]')
  • Simer Twilio Toronto developer
    Simer Twilio Toronto developer over 13 years
    @vivek he wrote i need to select the option not options
  • Franco
    Franco over 8 years
    This is the simplest way to get the default options of a selectbox
  • computercarguy
    computercarguy about 5 years
    I wasn't able to do this from the Chrome Console, but it works in the JavaScript file.
  • Panama Jack
    Panama Jack over 2 years
    Still the best answer in 2022.