Including the Previous Selection with the Current Selection - J Query

Problem
You have just manipulated a set of elements in order to acquire a new set of elements. However, you want to operate on the prior set as well as the current set.

Solution

You can combine a prior selection of DOM elements with the current selection by using the and Self() method. For example, in the following code, we are first selecting all <div>elements on the page. Next we manipulate this set of elements by finding all <p>elements contained within the <div>elements. Now, in order to operate on both the <div>and the <p>elements found within the <div>, we could include the <div>into the current set by using and Self(). Had I omitted the and Self(), the border color would have only been applied to the <p>elements:

Discussion
Keep in mind that when you use the and Self() method, it will only add into the current set being operated on and the prior set, but not all prior sets


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics