phil
phil

Reputation: 33

I'm trying to select the adjacent sibling of "this" in jquery

$(this+"p").slideDown("slow");

$(this)+$("p").slideDown("slow");

$("this+p").slideDown("slow");

does not work.

Upvotes: 3

Views: 2399

Answers (3)

Piotr Kula
Piotr Kula

Reputation: 9821

jQuery have not seemed to apply this? Possibly the syntax we are trying to use is incorrect.

next() can only select elements with an ID or Class - Not just a naked dom element as expected.

Instead use. > means select first level decends only.

$('body > div').hide();

But this gives the exact same result

$('body').children('div').hide();

But,

  • Next
  • $('body + div').hide();

and

  • Previous
  • $('body ~ div').hide();

Do not seem to work as expected? But jQuery use it as example for CSS selection...

Possibly there is a complex syntax to achieve this but I could not figure it out...

Upvotes: 0

S Pangborn
S Pangborn

Reputation: 12739

Yeah, your syntax is bad. You should use the jQuery Sibling function:

$(this).siblings().find("p").slideDown("slow");

The jQuery API site is awesome for looking stuff like this up, I rely on it nearly daily. I'd keep an eye on it.

Upvotes: 2

Tim Ridgely
Tim Ridgely

Reputation: 2420

Next.

$(this).next("p").slideDown("slow")

Make sure that the "p" element is directly adjacent, though. Otherwise you'll want to use nextAll.

Upvotes: 1

Related Questions