Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery DOM Get

JQ DOM Get

jQuery contains powerful methods for accessing HTML elements, attributes, and values.

DOM - Get Content

There are three simple, but useful, jQuery methods for accessing DOM content:

  • text() - returns the text content of selected elements
  • html() - returns the content of selected elements (including HTML markup or tags)
  • val() - returns the value of form fields
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
              <script>
                $(document).ready(function(){
                  $("#btn1").click(function(){
                    alert("Text: " + $("#test").text());
                  });
                  $("#btn2").click(function(){
                    alert("HTML: " + $("#test").html());
                  });
                  $("#btn3").click(function(){
                    alert("Value: " + $("#textField").val());
                  });
                });
              </script>
            </head>
            <body>
              <p id="test">This is paragraph with <b>bold</b> text.</p>
              <input type="text" id="textField" value="Lynxsia IT Solutions">
              <button id="btn1">test()</button>
              <button id="btn2">html()</button>
              <button id="btn3">val()</button>
            </body>
          </html>
        
      
DOM - Get Attribute

The jQuery attr() method is used to get attribute values.

The following example demonstrates how to get the value of the href attribute in a link:

					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
              <script>
                $(document).ready(function(){
                  $("#btn1").click(function(){
                    alert("Text: " + $("#link").attr('href'));
                  });
                });
              </script>
            </head>
            <body>
              <a id="link" href="https://lynxsia.com/">lynxsia.com</a>
              <button id="btn1">attr()</button>
            </body>
          </html>
        
      
DOM - Get data- Attribute

Though attr() method can be used to access any attribute of HTML elements such as href, class, id, data, aria, etc.

The data() method is used to access data attribute specially.

The data attribute is defined by the writing data followed by hyphen (-) and then valid name of the data attribute:

Syntax
					 
        
          <element data-name-of_Data="value">
        
      
Example
					 
        
          <!DOCTYPE html>
          <html>
            <head>
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
              <script>
                $(document).ready(function(){
                  $("#btn1").click(function(){
                    alert("Text: " + $("#test").data('user'));
                  });
                });
              </script>
            </head>
            <body>
              <p id="test" data-user="Lynxsia IT Solutions">This is paragraph</p>
              <button id="btn1">data()</button>
            </body>
          </html>
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved