jQuery – Get Content & Attributes

jQuery contains powerful method for changing and manipulating HTML elements & attributes

jQuery DOM Manipulation

One of the important part of jQuery manipulate the DOM.

We used “getElementById()” for accessing and finding the html element and also we used “innerHTML” for manipulate , replacing or getting the contents element.

<h3 id="demo">This the first page</h3>

<script>
  
  document.getElementById('demo').innerHTML = "Starting new page";
  
</script>

Output: Starting new page

Get Content – text(), html() and val()

This three element used for manipulate DOM:

text()This text return text and set values selected elements
html()This html return html and set values selected elements
val()This valu return value and set values form fields
<p id="demo">This is the <b>first page</b> text</p>
<button id="btn1">text button</button>
<button id="btn2">html button</button>

<script>

$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " + $("#demo").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#demo").html());
});
});

</script>

The following example demonstrates how to get the value of an input field with the jQuery val() method:

$(“#btn1”).click(function(){
  alert(“Value: ” + $(“#test”).val());
});

4,794 thoughts on “jQuery – Get Content & Attributes