Showing posts with label Getting dropdown value using JQuery. Show all posts
Showing posts with label Getting dropdown value using JQuery. Show all posts

Tuesday, July 21, 2015

Getting dropdown value using JQuery

This is how my HTML looks 

Here is the HTML and JS

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-2.1.4.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#selectedCountries").change(function () {

                var selectedOption = $("#selectedCountries option:selected");

                $(divResults).html('Value = ' + selectedOption.val() + ",Text = " + selectedOption.text());

            });

        });

    </script>

</head>

<body>

    Country : 

<select id="selectedCountries">
<option value="USA">United States</option>
<option value="India">India</option>
<option value="UK">United Kingdom</option>
<option value="CA">Canada</option>
<option value="AU">Australia</option>


</select>

<br/><br/>

<div id="divResults"> 


</div>

</body>

</html>


Results : If dropdown is selected, it will show its value and text as follows