How can I find the next <div> with the same class as the current one.
I have a <div> with class="help", now when some clicks on a button inside this <div> I want to select the next <div> with the same “help” class.
<div class="help">
<div>....OTHER HTML CONTENT......<div>
<input type='submit' class='ok'>
</div>
<div>....OTHER HTML CONTENT......<div>
<div class="help"></div>
<div>....OTHER HTML CONTENT......<div>
<div>....OTHER HTML CONTENT......<div>
<div>....OTHER HTML CONTENT......<div>
<div class="help"></div>
<div>....OTHER HTML CONTENT......<div>
<div class="help"></div>
Use a combination of
nextAll()and:firste.g.:
next()will only search the immediate sibling.You can of course, use a combination of
next()and a loop, e.g.EDIT
next()andnextAll()will only search siblings — ie elements on the same level. For example:So to get it to work with a multi-level layout, you need to first use some combination of
parent()andparents(), which allow you to navigate one or more levels ‘upwards’.parent()will traverse one level up:So in your particular example, starting from the button, you want to traverse one level up so you are on the same level as the other
<div class='help'>, then usenextAll()to find the next div.Assuming of course, you are handling the click event of the input: