Given the following HTML structure:
<div id="wrapper">
<div id="top">
<a href="http://example.com" class="link">click here</a>
</div>
<div id="middle">
some text
</div>
<div id="bottom">
some text
</div>
<div>
How would I select the bottom div (div#bottom) using JQuery when a user clicks on the link in the top div (div#top)? There will be several of these divs on the page, so I can’t just use $('#bottom'). $(this).next('#comment-form') also did not work.
You say that you’re duplicating
idattributes. Don’t do that, use classes instead:Your
idattributes must be unique within each page or you have invalid HTML and things won’t work very well.Once you’ve fixed your HTML, things will go much easier:
The
closestfunction goes up the DOM tree until it finds a match for the supplied selector, thenfindcomes back down to get the.bottomelement.