Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery capture


May 07, 2021 jQuery


Table of contents


jQuery - Get content and properties


jQuery has a powerful way to manipulate HTML elements and properties.


jQuery DOM operation

A very important part of jQuery is the ability to operate the DOM.

jQuery provides a range of DOM-related methods that make it easy to access and manipulate elements and properties.

jQuery capture DOM - Document Object Model (Document Object Model)

DoM defines the criteria for accessing HTML and XML documents:

"The W3C document object model is independent of the platform and language interface, allowing programs and scripts to dynamically access and update the content, structure, and style of the document."



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

Three simple and practical jQuery methods for DOM operations:

  • text() - Set or return the text content of the selected element

  • html() - Set or return the contents of the selected element (including HTML tags)

  • val() - Set or return the value of the form field

The following example shows how to get content through jQuery text() and html() methods:

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

Try it out . . .

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

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

Try it out . . .



Get Properties - attr()

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

The following example shows how to get the value of the href property in the link:

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

Try it out . . .

The next chapter explains how to set (change) content and property values.

Related articles

jQuery Reference Manual - Document operation

jQuery Reference Manual - Property operation

jQuery Reference Manual - CSS operation