Get the Next Element of a Class with jQuery

By Forrest Smith - Drempd.com

I\’ve been looking for a good way of finding the next element with a given class name. For example, given a user clicking on the link of \’click here 1\’, I would love to be able to target the next div that has a div class of \’result-here\’. Note, I don\’t want jQuery to return any of the other divs around it, and I don\’t want it to hit the second \’result-here\’ div under the \’Click Here 2\’ link.

<a href=\'#\' class=\"click-here\" id=\"click-here-1\">Click Here 1</a>
<div class=\"misc-div1\"></div>
<div class=\"misc-div2\"></div>
<div class=\"result-here\"></div>
<div class=\"misc-div3\"></div>

<a href=\'#\' class=\"click-here\" id=\"click-here-2\">Click Here 2</a>
<div class=\"misc-div1\"></div>
<div class=\"misc-div2\"></div>
<div class=\"result-here\"></div>
<div class=\"misc-div3\"></div>

I\’m actually hoping that there is a better way of doing this, but the best I\’ve come up with so far is as follows:

$(this).nextUntil(\'.result-here\').andSelf().last().next().css(\"background-color\", \"blue\");

Basically this is just saying, find all of the elements between the link and the \’result-here\’ div (include itself). Then only return the last one, and then target the next one. This should give the \’result here\’ div a background of blue. Again, not sure if this is the best way of doing this since it\’s seems way too complex, but it seems to work for now. If anyone has a more obvious way, please let me know!