2

This has probably been answered, but I am unable to find the answered question anywhere...

Assuming we have the following HTML...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Dropdown Test</title>
    </head>
    <body>
        <select name="myDropDownListName" id="myDropDownListID" class="dropdown">
            <option selected="selected" value="0">Please select a value...</option>
            <option value="1">My Custom Value 1</option>
            <option value="2">My Custom Value 2</option>
            <option value="3">My Custom Value 3</option>
        </select>
    </body>
</html>

What would the JQuery command look like to set 'My Custom value 2' to be the currently selected option in the dropdown list box, assuming I do not know the index 'value' value, and can only identify the item by the text 'My Custom Value 2'?

barrypicker
  • 9,740
  • 11
  • 65
  • 79
  • This will help you [link](http://stackoverflow.com/a/3744323/31901650) to get the value. – Gaurav Kalyan Dec 15 '14 at 20:11
  • possible duplicate of [jquery : How to select an option by its text?](http://stackoverflow.com/questions/3744289/jquery-how-to-select-an-option-by-its-text) – haxtbh Dec 15 '14 at 20:12
  • Answered here: https://stackoverflow.com/questions/17856523/how-to-set-the-selected-option-of-a-select-dropdown-list-with-jquery/17856569#69857266 – vapcguy Nov 08 '21 at 17:41

5 Answers5

5

You can use jquery .filter():

$('#myDropDownListID option').filter(function() {
    //you can use this.innerText too
    return $(this).text() === 'My Custom Value 2';
}).prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="myDropDownListName" id="myDropDownListID" class="dropdown">
  <option selected="selected" value="0">Please select a value...</option>
  <option value="1">My Custom Value 1</option>
  <option value="2">My Custom Value 2</option>
  <option value="3">My Custom Value 3</option>
</select>
Alex Char
  • 32,879
  • 9
  • 49
  • 70
  • I liked all the answers, but I think this one is best. It seems my example is too trivial compared to my real-world problem. The other answers use the 'contains' operator which may find a partial match, whereas this one is succinct and explicit. Also, this example references the dropdown by id, not by element type. – barrypicker Dec 15 '14 at 20:37
  • I agree but "Also, this example references the dropdown by id, not by element type" is the same, you can put #myDropDownListID instead of select. – sbaglieri Dec 15 '14 at 20:45
0

Simply like this :

$('select').val($("select option:contains('My custom Value 2')").val());
Mathieu Labrie Parent
  • 2,598
  • 1
  • 9
  • 10
0

Another way... use the contains selector to search for a DOM elem by its content.

$('select>option:contains("My Custom Value 2")').prop('selected', true);
sbaglieri
  • 319
  • 2
  • 10
0

People like to say to use .val(), but as you noticed, it doesn't like to set by text, but by using the index, instead. So you should do a find to get that, then set by it. But even that is only part of the story. You should first deselect all other options, and set the attribute of the option you want as selected using the index that you find.

Btw, I hate the $('#myDropDownListID') syntax because it is useless in SharePoint because it auto-generates GUIDs and puts them after the IDs, forcing you to have to grab it with $('[id*=myDropDownListID]'), with the * indicating that it contains that value, so that is how I will set this up, except I'll leave out the * because it's unnecessary in this case. But this syntax is also very useful if you want to use $ instead of * to say it starts with that value, and you can use title or name instead of id, so it is incredibly versatile, and I wish more people used it.

$(document).ready(function() {
    var yourText = "My Custom Value 2";

    // Remove all 'selected' attributes from all options
    $('select[id="myDropDownListID"] option').removeAttr('selected');

    // Get the index for the value you want to set
    var idx = $('select[id="myDropDownListID"] option').filter(function() {
        return $(this).html() == yourText;
    }).val();

    // Set the dropdown value by index and set it as selected by text
    var dropdownBox = $('select[id="myDropDownListID"]');
    dropdownBox.val(idx);
    dropdownBox.find('option[value="' + yourValue + '"]').attr('selected','selected'); // note that .val() doesn't do this
    dropdownBox.click(); // may be useful and necessary for certain engines to cache the value appropriately

    console.log(dropdownBox.html()); // should show you that the selected option is My Custom Value 2
    console.log(dropdownBox.val());  // should give you the index 2

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<select id="myDropDownListID">
    <option value="1">My Custom Value 1</option>
    <option value="2">My Custom Value 2</option>
    <option value="3">My Custom Value 3</option>
</select>
vapcguy
  • 7,097
  • 1
  • 56
  • 52
0

ddlListItems is ID of ListBox

if ($('#ddlListItems option:selected').text() == 'My Custom Value 2') {
                var itemsByValue = $('#ddlListItems option:selected').text();                   
            }