Hey,
I am trying to use JQuery to select the next element in a set of elements with the same class.
Here is the HTML setup:
<div class="sameClass selected">
<p>Text in here</p>
</div>
<div class="differentClass">
<p>Text in here
</div>
<div class="sameClass">
<p>Text in here</p>
</div>
When I have the first div with the class "sameClass" I would like to remove the "selected" class from the top div and apply it to the next div with the class "sameClass" so the results are like so:
<div class="sameClass">
<p>Text in here</p>
</div>
<div class="differentClass">
<p>Text in here
</div>
<div class="sameClass selected">
<p>Text in here</p>
</div>
I hope you get what I mean :)
UPDATE:
I have found that this one works the best.
$(".sameClass.selected").nextAll(".sameClass:first").andSelf().toggleClass("selected")
I have encountered one bug with it how ever, if the HTML is like so:
<p>
<div class="sameClass">
<p>Text in here</p>
</div>
<div class="differentClass">
<p>Text in here
</div>
<div class="sameClass selected">
<p>Text in here</p>
</div>
</p>
<p>
<div class="sameClass">
<p>Text in here</p>
</div>
</p>
It will not select the "sameClass" which is in the second P block. Any idea why this is happening?
Eef