JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



jQuery DOM

jQuery is a popular JavaScript library that simplifies the process of manipulating HTML documents. One of the key features of jQuery is its ability to manipulate the Document Object Model (DOM) of an HTML document. The DOM is a tree-like structure that represents the elements of an HTML document, and jQuery provides a simple and intuitive way to interact with this structure.

In this article, we will explore the basics of jQuery DOM manipulation, including selecting elements, modifying their attributes and content, and adding and removing elements from the DOM.

Selecting Elements

The first step in manipulating the DOM with jQuery is selecting the elements that you want to work with. jQuery provides a number of methods for selecting elements based on their tag name, class, ID, or other attributes.

For example, to select all the paragraphs in an HTML document, you can use the following code:


$('p');

This code uses the jQuery selector syntax to select all the <p> elements in the document. The result of this selection is a jQuery object that contains all the selected elements.

You can also select elements based on their class or ID attributes. For example, to select all the elements with the class "my-class", you can use the following code:


$('.my-class');

And to select the element with the ID "my-id", you can use the following code:


$('#my-id');

Modifying Attributes and Content

Once you have selected the elements that you want to work with, you can modify their attributes and content using jQuery methods.

For example, to change the text content of a paragraph element, you can use the text() method:


$('p').text('New text content');

This code selects all the <p> elements in the document and sets their text content to "New text content".

You can also modify the attributes of an element using the attr() method. For example, to change the href attribute of a link element, you can use the following code:


$('a').attr('href', 'http://www.example.com');

This code selects all the <a> elements in the document and sets their href attribute to "http://www.example.com".

Adding and Removing Elements

jQuery also provides methods for adding and removing elements from the DOM.

For example, to add a new paragraph element to the end of the body element, you can use the following code:


$('body').append('

New paragraph

');

This code selects the <body> element and appends a new <p> element with the text "New paragraph" to the end of it.

To remove an element from the DOM, you can use the remove() method. For example, to remove all the paragraphs with the class "my-class", you can use the following code:


$('.my-class').remove();

This code selects all the elements with the class "my-class" and removes them from the DOM.

Conclusion

jQuery DOM manipulation provides a powerful and intuitive way to interact with the elements of an HTML document. By selecting elements, modifying their attributes and content, and adding and removing elements from the DOM, you can create dynamic and interactive web pages with ease.

References

Activity