How to check dropdown selected or not in javascript

Select elements typically have two values that you want to access. First there's the value to be sent to the server, which is easy:

The second is the text value of the select. For example, using the following select box:

1

2

3

4

5

6

7

<option value="1">Mr</option>

<option value="2">Mrs</option>

<option value="3">Ms</option>

<option value="4">Dr</option>

<option value="5">Prof</option>

If you wanted to get the string "Mr" if the first option was selected (instead of just "1") you would do that in the following way:

1

2

$( "#myselect option:selected" ).text();

Topic: JavaScript / jQueryPrev|Next

Answer: Use the jQuery :selected Selector

You can use the jQuery :selected selector in combination with the val() method to find the selected option value in a select box or dropdown list.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Selected Option Value</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("select.country").change(function(){
        var selectedCountry = $(this).children("option:selected").val();
        alert("You have selected the country - " + selectedCountry);
    });
});
</script>
</head> 
<body>
    <form>
        <label>Select Country:</label>
        <select class="country">
            <option value="usa">United States</option>
            <option value="india">India</option>
            <option value="uk">United Kingdom</option>
        </select>
    </form>
</body>
</html>

If the value for an option is not defined specifically, the text content of the <option> element will be used as a value instead, as demonstrated in the following example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Selected Option Text</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("select.country").change(function(){
        var selectedCountry = $(this).children("option:selected").val();
        alert("You have selected the country - " + selectedCountry);
    });
});
</script>
</head> 
<body>
    <form>
        <label>Select Country:</label>
        <select class="country">
            <option>United States</option>
            <option>India</option>
            <option>United Kingdom</option>
        </select>
    </form>
</body>
</html>

Alternativley, you can use the jQuery text() method to get the text content of an element.


Get Selected Options from Multiple Select Box

Similarly, you can retrieve the selected values from multiple select boxes with a little trick.

A multiple select box allows a user to select multiple options. Hold down the control key on Windows or command key on Mac to select multiple options. You can enable multiple section in a select box by adding the attribute multiple to the <select> tag. Here's an example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Multiple Selected Option Value</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function() {
    $("button").click(function(){
        var countries = [];
        $.each($(".country option:selected"), function(){            
            countries.push($(this).val());
        });
        alert("You have selected the country - " + countries.join(", "));
    });
});
</script>
</head>
<body>
    <form>
        <label>Country:</label>
        <select class="country" multiple="multiple" size="5">
            <option>United States</option>
            <option>India</option>
            <option>United Kingdom</option>
            <option>Brazil</option>
            <option>Germany</option>
        </select>
        <button type="button">Get Values</button>
    </form>
</body>
</html>


Here are some more FAQ related to this topic:

  • How to get value of selected radio button using jQuery
  • How to get the values of selected checkboxes in a group using jQuery
  • How to get the text inside an element using jQuery

How do you check whether the dropdown is selected or not?

Use the tagName property to check if an element is a select dropdown, e.g. if (select. tagName === 'SELECT') {} .

How do I get a dropdown selected value?

Get Dropdown Selected Value using JavaScript.
function GetMaster1Details().
var value = document. getElementById("<%=ddlMaster1. ClientID%>");.
var getvalue = value. options[value. selectedIndex]. value;.
var gettext = value. options[value. selectedIndex]. ... .
alert("value:-" +" "+ getvalue + " "+ "Text:-" +" "+ gettext);.

How do you validate a drop down list in HTML?

Validate (Check) HTML Select DropDownList using jQuery Inside the jQuery OnClick event handler, the HTML Select DropDownList object is referenced and if the selected value matches the value of the default item then an error message is displayed using JavaScript alert message box.

How check dropdown is empty or not in jQuery?

The Best Answer is val() === "") { // ... }