How To Get Input Value In jQuery
Last Updated : Mar 11, 2024
In this tutorial we will show you the solution of how to get input value in jQuery, as we know for collects user input value we have to use val() method. By using this we can collect any type of input values from user.
Here first we needs to define input tag for creates input field to fill user and using this method we can collect then we displayed user input on alert box by using alert() method.
Step By Step Guide On How To Get Input Value In jQuery :-
In html we defined input element with placeholder attribute and button ‘Submit’ helps us for proceeds defined process.
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 appended click() method on button ‘Submit’ and within this we specified input element then which is appends with val() method for collects input value then result displayed on webpage using alert() method.
<!DOCTYPE html> <html> <head> <title>Get input value</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var v=$('input').val(); alert("User Name is "+v); }) }); </script> <body> <input type="text" placeholder="Enter Name"> <br><br> <button>Submit</button> </body> </head> </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.
- We defined input element with ‘placeholder’ attribute with value ‘Enter Name’ and button ‘Submit’ defined for proceeds defined process when user clicks on it.
- In script we defined ready() method within that we appended click() method on button ‘Submit’ by tag name ‘button’.
- There we specified input element by ‘input’ then which is appends with val() method, which returns users input value. It is stored on variable ‘v’ then we concatenates result with text ‘User Name is’ on alert() method for shows result on alert box.
- 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 input value 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 empty input field and button ‘Submit’ now user needs to fill input field then clicks on button. Result of ‘User Name is’ with user input of name we can see on browsers alert box.
I hope this tutorial on how to get input value in jQuery helps you and the steps and method mentioned above are easy to follow and implement.