The jquery library contains a number of ways to change and manipulate HTML elements and their attributes.
One very important part of this is that jquery can be used to manipulate the DOM.
This article describes using jquery to get the values or attributes of a DOM node element.
One of the three simple and useful methods are as follows:
Text () – Sets or gets the textual content of the specified element.
HTML () – Sets or gets the contents of the specified element (including HTML markup)
Val () – Sets or gets the value of an input field for the form.
For example, the following code uses the HTML () and text () methods to get the contents of an HTML element:
$ ("#btn1"). Click (function () {
alert ("Text:" + $ ("#test"). Text ();
});
$ ("#btn2"). Click (function () {
alert ("HTML:" + $ ("#test"). html ();
});
The following code gets the contents of the input in form:
$ ("#btn1"). Click (function () {
alert ("Value:" + $ ("#test"). Val ());
In addition to the above method, the attr () method is used to obtain the attributes of an element:
The following code is used to get the href attribute of the link:
<! DOCTYPE html>