93

I have a dropdown as:

<select id="HowYouKnow" >
  <option value="1">FRIEND</option>
  <option value="2">GOOGLE</option>
  <option value="3">AGENT</option></select>

In the above dropdown i know the text of the dropdown. How can set the value of the dropdown in document.ready with the text using jquery?

Prasad
  • 58,881
  • 64
  • 151
  • 199

11 Answers11

226

This is a method that works based on the text of the option, not the index. Just tested.

var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');

Or, if there are similar values (thanks shanabus):

$("#HowYouKnow option").each(function() {
  if($(this).text() == theText) {
    $(this).attr('selected', 'selected');            
  }                        
});
Peter J
  • 57,680
  • 8
  • 38
  • 45
  • 11
    This does not work in the case where your `select` list contains `options` with similar text values, ie `` - thoughts on how to handle this? – shanabus Jun 12 '12 at 20:23
  • 1
    To handle the similar values prolem you can get the first match with the index [0] (the default returns the last index) like this: $("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected')[0]; – Asmussen Dec 05 '12 at 03:17
  • 4
    The above methods are broken in jquery from version 1.9.1 and newer. Change .attr to .prop -> test here: http://jsfiddle.net/RB5wU/579/ – thomas nn Aug 30 '13 at 15:34
  • Your fiddle works for me. .attr isn't deprecated yet. – Peter J Sep 05 '13 at 19:53
  • If it isn't multiple select, break each loop with return false from anonymous function. – Line Nov 28 '14 at 13:18
  • It should be noted that, if possible, you should create the markup so that the *values* of your options match up with what you're trying to set. Then you can set the value of the select with jQuery. If your options used values that you knew, such as: ` – cloudworks Aug 17 '15 at 18:24
17

For the exact match use

    $("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');

contains is going to select the last match which might not be exact.

Parham
  • 1,037
  • 11
  • 17
  • 1
    In my case `.attr()` did changed `selected` option in dom but text displayed did not changed, `.prop()` on other hand would do both things at a time. – abdul qayyum Nov 28 '19 at 08:48
4

try this..

$(element).find("option:contains(" + theText+ ")").attr('selected', 'selected');
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
Divy
  • 41
  • 1
4
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
Community
  • 1
  • 1
Vinay saini
  • 57
  • 1
  • 1
2
var myText = 'GOOGLE';

$('#HowYouKnow option').map(function() {
    if ($(this).text() == myText) return this;
}).attr('selected', 'selected');
1

For GOOGLE, GOOGLEDOWN, GOOGLEUP i.e similar kind of value you can try below code

   $("#HowYouKnow option:contains('GOOGLE')").each(function () {

                        if($(this).html()=='GOOGLE'){
                            $(this).attr('selected', 'selected');
                        }
                    });

In this way,number of loop iteration can be reduced and will work in all situation.

juhi
  • 320
  • 1
  • 3
  • 15
1

The below code works for me -:

jQuery('[id^=select_] > option').each(function(){
        if (this.text.toLowerCase()=='text'){
            jQuery('[id^=select_]').val(this.value);
        }
});

jQuery('[id^=select_]') - This allows you to select drop down where ID of the drop down starts from select_

Hope the above helps!

Cheers S

stevensagaar
  • 626
  • 4
  • 15
0

Here is an simple example:

$("#country_id").change(function(){
    if(this.value.toString() == ""){
        return;
    }
    alert("You just changed country to: " + $("#country_id option:selected").text() + " which carried the value for country_id as: " + this.value.toString());
});
fullstacklife
  • 2,031
  • 3
  • 16
  • 13
-1
$("#HowYouKnow").val("GOOGLE");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
dell
  • 81
  • 1
  • 1
  • 4
  • $(document).ready(function() { $("#HowYouKnow").val("GOOGLE"); }); Sets the value too... not sure why I got down voted! – dell Dec 11 '09 at 16:16
  • 13
    Maybe because that only works if your `option` value is the same as the text. So in the OP's example, the GOOGLE text option has a value of `2` so it does not work. – shanabus Jun 12 '12 at 20:26
  • 1
    This will only set based on the value box, not based on the dropdowns text. – BrettC Feb 28 '18 at 16:18
-1
$("#HowYouKnow option:eq(XXX)").attr('selected', 'selected');

where XXX is the index of the one you want.

helloandre
  • 10,541
  • 8
  • 47
  • 64
-1

This is worked both chrome and firefox

set value in to dropdown box.

var given = $("#anotherbox").val();
$("#HowYouKnow").text(given).attr('value', given);
Naveenbos
  • 2,532
  • 3
  • 34
  • 60