Get the Next Element of a Class with jQuery

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!




Related Posts


Simple Sticky Element

The user scrolls down the page and once the scrolling reaches the top of an element of my choosing, that element sticks to the top of the window, so it's always visible from that point on...
Read Post

Removing the Horizontal Margins on a Google Chart Graph

I seem to be able to successfully modify quite a bit with google charts, but I couldn't find a good way to remove the horizontal padding to the left and right of a google graph. From what I can find, there really isn't a good way to do it...
Read Post

PHP JSON Returned As Object Instead of Array

I was returning, what I thought was an array from PHP, but it kept returning it as a json object...
Read Post

A Simple Slideout Menu

I just wanted an easy slideout menu. Click on a link, and the menu slides out from the left side of the screen. Click on the link again, or somewhere else on the page, and the menu slides back in...
Read Post