jQuery Topics
- jQuery Introduction
- jQuery Callbacks
- jQuery deferred
- jQuery selectors
- jQuery Ajax Events
- jQuery Ajax Methods
- jQuery Keyboard Events
- jQuery Keyboard Methods
- jQuery Form Events
- jQuery Form Methods
- jQuery Mouse Events
- jQuery Mouse Methods
- jQuery Event Properties
- jQuery Event Methods
- jQuery HTML
- jQuery CSS
- jQuery Fading
- jQuery Traversing
- jQuery Utilities
- jQuery Properties
jQuery .add() Method
Photo Credit to CodeToFun
🙋 Introduction
jQuery is renowned for its ability to simplify JavaScript code and expedite web development processes. Among its arsenal of methods, the .add()
method stands out as a versatile tool for dynamically adding elements to selected sets.
This guide explores the potential of the jQuery .add()
method through illustrative examples and clear explanations, empowering you to wield this powerful tool proficiently in your web projects.
🧠 Understanding .add() Method
The .add()
method in jQuery allows you to add elements to the set of matched elements. It seamlessly merges selected elements with additional elements or selectors, facilitating dynamic manipulation of DOM elements.
💡 Syntax
The syntax for the .add()
method is straightforward:
$(selector).add(elements)
📝 Example
Adding Elements to Selected Set:
Consider a scenario where you have a set of paragraphs and you want to add a heading element to it. You can accomplish this using the
.add()
method:index.htmlCopied<div id="content"> <p>Paragraph 1</p> <p>Paragraph 2</p> </div>
example.jsCopiedvar heading = $("<h2>New Heading</h2>"); $("#content").find("p").add(heading).appendTo("#content");
This code will add a <h2> heading element alongside the existing paragraphs within the #content div.
Combining Sets of Elements:
You can also use the
.add()
method to combine multiple sets of elements. Suppose you have two sets of list items and you want to merge them:index.htmlCopied<ul id="list1"> <li>Item 1</li> <li>Item 2</li> </ul> <ul id="list2"> <li>Item 3</li> <li>Item 4</li> </ul>
example.jsCopiedvar combinedList = $("#list1 li").add("#list2 li");
Now combinedList will contain all list items from both #list1 and #list2.
Chaining .add() Method:
The
.add()
method can be chained with other jQuery methods for concise and efficient code. For instance, let's add a class to a set of elements after combining them:example.jsCopiedvar combinedElements = $("#element1").add("#element2").addClass("highlight");
This code will add the class highlight to both #element1 and #element2.
Removing Duplicates:
The
.add()
method automatically removes duplicates when adding elements, ensuring that each element appears only once in the resulting set.
🎉 Conclusion
The jQuery .add()
method offers a flexible and efficient way to manipulate sets of DOM elements. Whether you need to add elements, combine sets, or chain methods for complex operations, .add()
empowers you to achieve your desired functionality with ease.
By mastering the usage of this method, you can streamline your JavaScript code and create more dynamic and interactive web experiences.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery .add() Method), please comment here. I will help you immediately.