All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Get Input Value By ID

Last Updated : Mar 11, 2024

jQuery Get Input Value By ID

In this article we will show you the solution of jQuery get input value by id, as we know in jquery for get elements 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 element for creates 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 jQuery Get Input Value By ID :-

In html we defined input element with placeholder, id attributes and button ‘Get Value’ helps us for proceeds 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 ‘Get Value’ and within this we specified input element then which is appends with val() method for collects input value and stored on variable ‘v’ then which is displayed on webpage using alert() method.

<!DOCTYPE html>
<html>
    <head>
        <title>Get input value by ID</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $(".gv").click(function(){
                    var v=$('#inp').val();
                    alert("Your Age is "+v);
                })
            });
        </script>
    <body>
        <input type="text" placeholder="Enter Age" id="inp">
        <br><br>
        <button class="gv">Get Value</button>
    </body>
    </head>
</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. We defined input element with ‘placeholder’,’id’ attributes with respected values ‘Enter Age’,’inp’, as we know <br> refers break tag and button ‘Get Value’ defined with class attribute ‘gv’ for proceeds process when user clicks on it.
  8. In script we defined ready() method within that we appended click() method on button ‘Get Value’ by class attribute ‘gv’.
  9. There we specified input element by id ‘inp’ then which is appends with val() method for which returns users input value. It is stored on variable ‘v’ then we concatenates ‘v’ with text ‘Your Age is’ on alert() method for shows result on alert box.
  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 input value by using id 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 ‘Get Value’ now user needs to fill input field then clicks on button.

Result of ‘Your Age is’ with user input we can see on browsers alert box.

I hope this article on jQuery get input value by id helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪