jQuery | Get Content and Attributes
Get Content: In order to get the content of DOM object, there are three simple methods.jQuery methods for DOM manipulation which are listed below:
- text(): It is used to set or return the text content of selected elements.
- html(): It is used to set or return the content of selected elements including HTML markup.
- val(): It is used to set or return the value of form fields.
Example: This example uses text content method to get the content.
<!DOCTYPE html> <html> <head> <title>jQuery Get Content</title> <script src= </script> </head> <body style="text-align:center;"> <h1 id="GFG1" style = "color:green;"> GeeksForGeeks </h1> <h2 id="GFG2">jQuery Get Content</h2> <button id="btn1">Text</button> <button id="btn2">HTML</button> <!-- Script to get the content --> <script> $(document).ready(function(){ $("#btn1").click(function(){ alert("Text: " + $("#GFG2").text()); }); $("#btn2").click(function(){ alert("HTML: " + $("#GFG1").html()); }); }); </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:

- After click on the Text button:

- After click on the Html button:

Get Attributes The jQuery attr() method is used to get attribute values of DOM objects.
Example: This example uses attr() method to get the attribute value.
<!DOCTYPE html> <html> <head> <title>jQuery Get Attributes</title> <script src= </script> </head> <body style="text-align:center;"> <h1 style = "color:green;"> GeeksForGeeks </h1> <h2>jQuery Get Attributes</h2> <button id="btn1">Click</button> <br><br> <h3> geeksforgeeks.org </a> </h3> <!-- Script to get the attribute value --> <script> $(document).ready(function(){ $("button").click(function(){ alert($("#GFG").attr("href")); }); }); </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:

- After click on the button:

Recommended Posts:
- jQuery | Set Content and Attributes
- How to copy the content of a div into another div using jQuery ?
- How to detect and change the content/style of a div using jQuery ?
- JQuery | Detect a textbox content is changed or not
- Content editable change event in jQuery
- How to check a selector matches some content using jQuery?
- How to insert HTML content into an iFrame using jQuery?
- XML | Attributes
- HTML | Attributes
- HTML | Id Attributes
- PHP | SimpleXMLElement attributes() Function
- HTML | DOM attributes Property
- HTML | data-* Attributes
- HTML | Attributes Complete Reference
- How to get the data attributes of an element using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



