I have some repeating divs that I'm trying to .toggleClass multiple elements on and am having trouble pointing to the right ones without altering others.
Basically I have bunch of divs with the same ids and classes and I only want to toggle each to expand by clicking a link with the class "a_expander". I would like that to toggle the #result class between "a_collapse a_expand, as well as toggle the #resthumb between "rescellLc rescellLe" it looks like this
<div id="result" class="bg_fader scaler a_collapse">
<div id="resthumb" class="scaler rescellLc">
<img src="img/fine-fur-coat.png" />
</div>
<div class="rescellC">
<div class="res_content_album-title">
<h><a class="a_expander">Fine Fur Coat</a></h>
<p>3 Tracks | September 2012</p>
</div>
</div>
<div id="result" class="bg_fader scaler a_collapse">
<div id="resthumb" class="scaler rescellLc">
<img src="img/tunneling.png" />
</div>
<div class="rescellC">
<div class="res_content_album-title">
<h><a class="a_expander">Tunneling</a></h>
<p>15 Tracks | July 2011</p>
</div>
</div>
</div>
and my js so far was
$('.a_expander').click(function () {
$(this).siblings('#result').toggleClass("a_collapse a_expand");
$(this).siblings('.resthumb').toggleClass("rescellLc rescellLe");
});
I was using the .parent() to get to the outer div but could not change the #resthumb only point to the #result. Hopefully this is not to confusing and someone can help. Much appreciated in advance.
Ok, lots of things here...
First:
I like the tunes from Emily Peal :).
Second:
I recommend that you consider restructuring your HTML -- the sample structure and style names are pretty confusing when trying to figure it out. If you are willing to restructure, I put together an example that does effectively accomplishes the same in a much cleaner way here:
http://jsfiddle.net/9PFps/2/ .
It's cleaner to just do the toggle in one place (for example at the album level) and let the children nodes leverage that to adjust their style. Take a look at the example and you'll see what I mean. BTW, I also added "Bang Bang" and some images for the covers to my example for the sake of completeness. :)
Here's the snippets:
HTML:
JavaScript:
CSS:
Third:
Your existing HTML above isn't well-formed -- you are missing a closing div in your first result. This would lead to problems with jQuery selectors (and likely your layout), depending on what methods you're using.
Finally:
If you really want to use that existing structure, I put together an example of walking the DOM to the nodes whose classes you want to toggle, with some made up styles. You can find it here: http://jsfiddle.net/KSeLL/
Good luck!