All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Selected Value Of Dropdown In jQuery On Change

Last Updated : Mar 11, 2024

Get Selected Value Of Dropdown In jQuery On Change

In this article we will show you the solution of get selected value of dropdown in jQuery on change, here we needs to use change() and alert() methods.

The change() method triggers the change event, or attaches a function to run when a change event occurs. As we know the alert() method used for display anything on webpage in default alert box in browser.

Step By Step Guide On Get Selected Value Of Dropdown In jQuery On Change :-

Here we defined select tag with three options it contain programming languages ‘PHP, JAVA, PYTHON’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser.

There we defined change() method which is appends with select tag and there we defined some string using alert() method then ‘this.value’ refers selected option by user.

<!DOCTYPE html>
<html>
    <head>
        <title>CHANGE DROP DOWN SELECTED VALUE</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                    $("#sel").change(function(){
                        alert("Selected Option is "+this.value);
                    });
                })
        </script>
        </head>
        <body>
            <select id="sel">
                <option value="PHP">PHP</option>
                <option value="JAVA">JAVA</option>
                <option value="PYTHON">PYTHON</option>
            </select>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we defined change() method which is appends with select tag by using id ‘sel’.
  8. There we defined message ‘Selected Option is’ with ‘this.value-refers changed option value’ on alert() method for shows alert message will display for user verifications.
  9. In html block we defined select tag with id ‘sel’ and options ‘PHP, JAVA, PYTHON’ with respected values.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get selected value from drop down list on change in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see drop down box with options user needs to change their options then alert message will appear with that value like ‘Selected Option is JAVA’.

I hope this tutorial on get selected value of dropdown in jQuery on change helps you and the steps and method mentioned above are easy to follow and implement.