WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

jQuery - Get Content and Attributes


jQuery contains incredible strategies for changing and controlling HTML components and attributes.


jQuery DOM Manipulation

One imperative piece of jQuery is the likelihood to control the DOM.

jQuery accompanies a group of DOM related techniques that make it simple to get to and control components and attributes.


Get Content - content(), html(), and val()

Three straightforward, yet valuable, jQuery techniques for DOM control are:

  • text() - Sets or returns the content substance of chose elements
  • html() - Sets or returns the substance of chose components (counting HTML markup)
  • val() - Sets or returns the estimation of structure fields

The following model exhibits how to get content with the jQuery content() and html() methods:

Example

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

The following precedent exhibits how to get the estimation of an information field with the jQuery val() method:

Example

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

Get Attributes - attr()

The jQuery attr() strategy is utilized to get property values.

The following precedent exhibits how to get the estimation of the href characteristic in a link:

Example

$("button").click(function(){
    alert($("#w3s").attr("href"));
});

The next section discloses how to set (change) substance and property values.