Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery selector problem about "+"

Today I found a strange jquery selector in the following code:

$(this).find("+div.parent").hide();

I've searched this in Jquery API and only found what pre_element+next_element means.What does the + do in the code?

Thanks.

like image 651
Young Avatar asked Jun 07 '11 08:06

Young


3 Answers

the selector + matches the element that follows the previous one

for example if you want to matches all the divs that are after bold text you can use this selector:

$("b+div")

so if $(this) is reference to <b>:

$(this).find('+div.parent')

will match all the div with class parent that are immediately after <b>

like image 191
Teneff Avatar answered Oct 13 '22 22:10

Teneff


the + is an Adjacent Sibling selector

it will select the immediate sibling of the this, it is equivalent to next()

$(this).find("+div.parent").hide();

is the same as

$(this).next("div.parent").hide();
like image 4
clairesuzy Avatar answered Oct 13 '22 21:10

clairesuzy


It'll find the div with the class parent adjacent to whatever $(this) is.

Fiddle here: http://jsfiddle.net/prbRA/1/

like image 2
Kevin Avatar answered Oct 13 '22 21:10

Kevin