Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Basic

jQuery Ajax Events

jQuery Ajax Methods

jQuery Keyboard Events

jQuery Keyboard Methods

jQuery Form Events

jQuery Form Methods

jQuery Mouse Event

jQuery Mouse Methods

jQuery Event Object

jQuery Fading

jQuery Document Loading

jQuery Traversing

jQuery Utilities

jQuery Property

jQuery HTML

jQuery CSS

jQuery Miscellaneous

jQuery .addBack() Method

Posted in jQuery Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 56 - Views
⏳ 4 mins
💬 1 Comment
jQuery .addBack() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery is renowned for its simplicity and efficiency in DOM manipulation. Among its arsenal of methods, the .addBack() method stands out as a powerful tool for refining and extending jQuery selections.

In this guide, we'll explore the .addBack() method in detail, understanding its syntax, usage, and practical examples to leverage its full potential in your web development projects.

🧠 Understanding .addBack() Method

The .addBack() method in jQuery allows you to add the previous set of elements to the current set of elements in the jQuery object. This is particularly useful when chaining multiple jQuery methods or when you want to include both the original selection and its ancestors or siblings in subsequent operations.

💡 Syntax

The syntax for the .addBack() method is straightforward:

syntax.js
Copied
Copy To Clipboard
.addBack([selector])

Parameters

  • [selector] (Optional): A selector expression to filter the elements that are added back.

📝 Example

  1. Adding Previous Elements to the Current Selection:

    Consider a scenario where you have a list of paragraphs, and you want to add the list items containing those paragraphs to your selection. You can achieve this using the .addBack() method as follows:

    index.html
    Copied
    Copy To Clipboard
    <ul>
      <li>Item 1</li>
      <li class="selected">Item 2</li>
      <li>Item 3</li>
    </ul>
    example.js
    Copied
    Copy To Clipboard
    $("li.selected").next().addBack().css("color", "red");

    This will change the color of both Item 2 and Item 3 to red, as .next() selects the next sibling of the .selected item, and .addBack() adds the original .selected item back to the selection.

  2. Filtering Elements to Add Back:

    You can also provide a selector expression to filter the elements that are added back. For instance, let's add back only the siblings of the selected list item:

    example.js
    Copied
    Copy To Clipboard
    $("li.selected").next().addBack("li").css("font-weight", "bold");

    This will make both the selected list item and its siblings bold.

  3. Chaining .addBack() with Other Methods:

    Combining .addBack() with other jQuery methods opens up a myriad of possibilities. Here's an example where we toggle a class on a parent element along with its children:

    index.html
    Copied
    Copy To Clipboard
    <div class="parent">
      <p>Child 1</p>
      <p class="selected">Child 2</p>
      <p>Child 3</p>
    </div>
    example.js
    Copied
    Copy To Clipboard
    $(".selected").parent().addBack().toggleClass("highlight");

    This will toggle the class highlight on both the selected child and its parent.

🎉 Conclusion

The .addBack() method in jQuery provides a convenient way to include both the original selection and its preceding set of elements in subsequent operations. Whether you're refining selections, chaining methods, or filtering elements, .addBack() enhances your ability to manipulate the DOM efficiently.

By mastering its usage, you can streamline your jQuery code and build more dynamic and interactive web applications.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy