All TalkersCode Topics

Follow TalkersCode On Social Media

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

Get Attribute Value jQuery

Last Updated : Mar 11, 2024

Get Attribute Value jQuery

In this article we will show you the solution of get attribute value jQuery, here we needs to use attr() and click() methods. The click() method used for loads some process when user clicks on button element defined html code and as we know attr() method used for sets or returns attributes and values of the selected elements.

So here we can get value of defined input element from html code.

Step By Step Guide On Get Attribute Value jQuery :-

Here we defined html block with input which is consist of three attributes ‘type, id, value’ and button ‘Get Value’ for generate some process when user clicks on it.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button tag name then there we specified input element by using id ‘nm’ which is appends with attr() method with ‘value’ attribute name for gets value of input element and it’s stored on variable ‘val’.

Using alert() method we printed result value on alert box.

<!DOCTYPE html>
<html>
    <head>
        <title>GET ATTRIBUTE 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 val=$('#nm').attr('value');
                    alert('Your input value is '+val);
                })
            })
        </script>
        </head>
        <body>
            <input type="text" id="nm" value="Henry Mark">
            <button>Get Value</button>
        </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 appending click() method with button by using tag name ‘button’ for generate click event when user clicks on ‘Get Value’ button.
  8. Within function we needs to specify input element by using id attributes value ‘nm’ then which is needs to appends with attr() method. In this method we need to pass ‘value’ attribute name for collects input element’s value attributes value.
  9. Then result stored on variable ‘val’ which is printed on alert box using alert method with string ‘Your input value is’ and we can also get dynamically value from user then displayed on alertbox.
  10. In html block we defined input element with id attribute ‘nm’, value attribute ‘Henry Mark’ and button ‘Get Value’.
  11. 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 attribute value from specific html element using 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 input element with value ‘Henry Mark’ and button ‘Get Value’ now user needs to click on button then we can see fetched input value displayed on alert box with string ‘Your input value is’.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪