jQuery GET or SET - HTML Contents and Attributes
jQuery includes a number of useful methods for altering and manipulating HTML elements and attributes.
jQuery DOM Manipulation
The ability to alter the DOM is a crucial feature of jQuery.
A number of DOM-related functions are included in jQuery, making it simple to access and edit html elements and attributes.
DOM = Document Object Model
The DOM establishes a standard for interacting with HTML and XML documents :
The W3C Document Object Model (DOM) is a platform and language-independent interface that enables programmes and scripts to dynamically access and alter a document's content, structure, and style.
Set or Get HTML Content - text(), html(), and val() Methods
Three basic yet important DOM manipulation jQuery techniques are:
text()
- The text content of selected components is set or returned.html()
- Sets or returns the content of the elements that have been specified (including HTML markup).val()
- The value of form input fields is set or returned.
Example:-
This is some bold text and underline in a paragraph.
Click the below button to see result.
Enter Name:
Output :-
This is some bold text and underline in a paragraph.
Click the below button to see result.
Enter Name:
Related Links
Set or Get Attributes - attr() Method
The attr()
function of jQuery is used to set or get attribute value of selected element.
Use the following syntax to assign single attribute:
css("attributename","value");
Use the following syntax to assign multiple attributes at once:
css({"attributename1":"value1","attributename2":"value2",...});
Example :-
Output :-
Tip:- Mouse over the link (or click on it) to see that the value of the href attribute has changed.
A Callback Function for attr() Method
A callback function is included with the jQuery method attr()
. The current element's index in the list of chosen items, as well as the original (old) attribute value, are both sent to the callback function. Then, from the function, you return the string you want to use for the new attribute value.
Related Links