Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine these jQuery statements?

I have a jQuery statement like this;

var current = $(this);
current.hide();
current.siblings('.ab').hide();
current.siblings('.cd').hide();

I want to change this into a single statement and I wrote;

$(current,current.siblings('.ab'),current.siblings('.cd')).hide();

But ab is not hiding. How can I combine the 3 hide() statements into one?

like image 810
Alfred Avatar asked Apr 10 '14 10:04

Alfred


People also ask

Can you combine jQuery and JavaScript?

It is possible to run jQuery and JavaScript together in you application. All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes. But, yes!

What is $$ in jQuery?

$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.

How to use not in jQuery selector?

jQuery :not() Selector The :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).


2 Answers

You can use a multiple selector and addBack():

$(this).siblings(".ab, .cd").addBack().hide();

addBack() will add the original element back into the set, so you can get both the element and its relevant siblings in the same jQuery object.

like image 86
Frédéric Hamidi Avatar answered Oct 22 '22 12:10

Frédéric Hamidi


You can use a multiple selector (comma separated) for the siblings function and than use addBack to include the first element.

Add the previous set of elements on the stack to the current set, optionally filtered by a selector.

Code:

current.siblings(".ab, .cd").addBack().hide();
like image 35
Irvin Dominin Avatar answered Oct 22 '22 12:10

Irvin Dominin