You can achieve this by using the .not()
and .addClass()
methods in jQuery.
Assuming that you want to add the class "opened" to all elements except the one that triggered the event, you can do the following:
index.tsx105 chars4 lines
Here, we're using event delegation to attach a click event to all elements with the class "my-element". When an element with that class is clicked, we'll add the "opened" class to all elements with that class, except for the one that triggered the event (i.e., this
).
The .not()
method is used to exclude the current element from the selection. We pass this
as the argument to .not()
, which will create a new jQuery object containing all elements with the "my-element" class except for the one that triggered the event.
Finally, we use the .addClass()
method to add the "opened" class to the remaining elements. This function will be executed only for the elements that are not the event target.
gistlibby LogSnag