jquery find next element with class

99,409

Solution 1

The problem is that your using the next traversing function rather than 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

Solution 2

Try this:

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

hope it helps. Sinan.

Share:
99,409
Loony2nz
Author by

Loony2nz

Updated on July 23, 2022

Comments

  • Loony2nz
    Loony2nz almost 2 years

    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>
    

    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.

  • Greg
    Greg over 14 years
    or you could always do .next().next()
  • Loony2nz
    Loony2nz over 14 years
    this sorta works. I only get the 2nd span tag with class of error, to have it's contents updated.
  • Loony2nz
    Loony2nz over 14 years
    @Sinan: nope. Nothing happens. $("button[disabled=disabled]").parent().find("span.error").t‌​ext("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.
  • Sprachprofi
    Sprachprofi about 12 years
    If you have several .error elements, you will need .nextAll(".error").first(). A bit counter-intuitive, but .next() is only for the physically next item, no chance to make conditions.
  • sksallaj
    sksallaj over 11 years
    No idea why can't next = nextAll("<class>").first(). And why the currently description for next be called "nextElement()". This is why naming functions that explains itself is extremely important in coding! I cant even fathom how many people get corrected by this..
  • Frank Nocke
    Frank Nocke about 7 years
    In other words: .nextAll() filters first by selector, then looks for the truly next one(s) within that set. — .next looks first for the next one(s), then filters that set down. The latter might i.e. be useful to handle sometimes-trailing <hr>s or such...