All TalkersCode Topics

Follow TalkersCode On Social Media

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

Get Data Attribute jQuery

Last Updated : Mar 11, 2024

Get Data Attribute jQuery

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

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

Step By Step Guide On Get Data Attribute jQuery :-

Here we defined html block with input element which is consist of three attributes ‘type, id, data-textval, value’ and button ‘Get Data 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 id ‘getdataval’ then there we specified input element by using id ‘nm’ which is appends with data() method with ‘textval’ for gets data value of input element and it’s stored on variable ‘txtval’.

Using text() method we appended result on h3 element.

<!DOCTYPE html>
<html>
    <head>
        <title>GET DATA ATTRIBUTE VALUE</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $('#getdataval').click(function(){
                    var txtval=$('#nm').data('textval');
                    $('#res').text('Your fetched data text value is '+txtval);
                })
            })
        </script>
        </head>
        <body>
            <input type="text" id="nm" data-textval="Hi Everyone!" value="Mark">
            <button id="getdataval">Get Data Value</button>
            <h3 id="res"></h3>
        </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 id ‘getdataval’ for generate click event when user clicks on ‘Get Data Value’ button.
  8. Within function we needs to specify input element by using id attributes value ‘nm’ then which is needs to appends with data() method. In this method we need to pass ‘textval’ for collects input element’s data-textval attributes value.
  9. Then result stored on variable ‘txtval’ which is appended using text() method with h3 element by specifying h3 element’s id ‘res’.
  10. In html block we defined input element with id attribute ‘nm’, value attribute ‘Henry Mark’, data-textval attribute ‘Hi Everyone!’ and button ‘Get Data 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 data attribute value 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 Data Value’ now user needs to click on button then we can see fetched input’s data attributes value ‘Hi Everyone!’ displayed at last with string ‘Your fetched data text value is’.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪