Get Selected Value Of Dropdown In jQuery On Change
Last Updated : Mar 11, 2024
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>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- Here we imported open source jquery library support file which is needed for coding using jquery in program.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined ready() method within that we defined change() method which is appends with select tag by using id ‘sel’.
- 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.
- In html block we defined select tag with id ‘sel’ and options ‘PHP, JAVA, PYTHON’ with respected values.
- 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.