56

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?

3
  • 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 ! Commented Jan 24, 2011 at 11:45
  • $("#myCombobox option[text='it\'s me']").attr("selected","selected"); Commented Jan 24, 2011 at 11:53
  • stackoverflow.com/questions/292615/… Try this one.. Commented Apr 16, 2013 at 8:29

9 Answers 9

109

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

Sign up to request clarification or add additional context in comments.

5 Comments

selected="selected" is also valid, and is what is suggested on W3Schools. +1
$('select').val('something') all that is required.
@halfcube-according to the question steven spielberg wants to select those options which text is 'it's me' not value
you are breaking the code dude check $('select option[text="it's me"]')
@vivek he wrote i need to select the option not options
47

You can just do this:

$('#myCombobox').val(1)

2 Comments

I wasn't able to do this from the Chrome Console, but it works in the JavaScript file.
Still the best answer in 2022.
7

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"

3 Comments

This answer is copied from stackoverflow.com/questions/496052/… and does not answer what Steven is asking.
Mark, $('select').val('Two'); will solve the problem.. what else Steven has asked?
Not sure I can think of a situation you'd want to set all selects to a particular value? :-) Except '0', maybe...
4
$('#userZipFiles option').prop('selected', function() {
        return this.defaultSelected;
    });     

1 Comment

This is the simplest way to get the default options of a selectbox
2
$("#dropdownList option[text='it\'s me']").attr("selected","selected"); 

1 Comment

The single quote in it's me needs escaping.
1
jQuery("select#cboDays option[value='Wednesday']").attr("selected", "selected");

Comments

0

One line of jQuery does it all!

$("#myCombobox option[text='it\'s me']").attr("selected","selected"); 

2 Comments

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?
@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 """ :(".
0

This is working fine:

$('#country').val($("#country option:contains('It\'s Me')").val());

1 Comment

Won't the use of double quotes inside double quotes create problems?
-1

$('#dropdownId').val("valueToBeSelected");

1 Comment

When answering older questions, please make sure you provide either a novel solution, or a significantly better explanation than existing answers. If an existing answer already has your preferred solution, consider upvoting that answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.