views:

654

answers:

3

I'm having a brain fart here...I'm trying to find the next element with a class of "error" and hitting a wall.

In looking at the demo on jQuery's site, this should work, but doesn't.

$("button[disabled]").next().text("this button is disabled");

<div><button disabled="disabled">First</button> - <span>no overwrite</span><span class="error"></span></div>

<div><button>Second</button> - <span></span></div>

<div><button disabled="disabled">Third</button> - <span>no overwrite</span><span class="error"></span></div>

Excuse the bad formatting...but I'm trying to find the span or div or whatever after the element in question, like the button above.

so the disabled button line should read, 'no overwrite this button is diabled'

I've tried

$("button[disabled]").next(".error").text("this button is disabled");

to no avail.

help :)

thanks!

+1  A: 

Try this:

$("button[disabled=disabled]").parent().find("span.error").text("this button is disabled");

hope it helps. Sinan.

Sinan Y.
@Sinan: nope. Nothing happens.$("button[disabled=disabled]").parent().find("span.error").text("this button is disabled");<div> <button disabled="disabled">First</button> - <span>no overwrite</span><span class="error"></span></div><div> <button>Second</button> - <span></span></div><div> <button disabled="disabled">Third</button> - <span>no overwrite</span><span class="error"></span></div>plug that into your browser and see if it works for you. The disabled buttons with span class='error' should be updated.
Loony2nz
A: 

next() won't work in this case because it has to be a sibling for that to work. In this case you need:

$("button[disabled]").parent().nextAll()
  .find("span.error:first").text("this button is disabled");
cletus
or you could always do .next().next()
Greg
this sorta works. I only get the 2nd span tag with class of error, to have it's contents updated.
Loony2nz
+1  A: 

The problem is that your using the next traversing function rather then nextAll

$("button[disabled]").nextAll(".error").text("this button is disabled");

When you use next its just looking at the "next" element which is

<span>no overwrite</span>

Next all looks at all siblings that are next

PetersenDidIt