tags:

views:

57

answers:

1

I have a simple jQuery function that toggles a div's visibility when clicked. The problem with the function is that it can't be applied to multiple items on the same page or they all toggle in tandem. Is there a way around this without having to assign a unique class to each instance?

jQuery:

 $(function(){
    $(".toggleThis").click(function(){
         $(".hiddenText").slideToggle("fast");
            $(this).html(function(i,html) {
                if (html.indexOf('+') != -1 ){
                   html = html.replace('+','-');
                } else {
                   html = html.replace('-','+');
                }
                return html;
            })
    });
}); 

html:

<p class="toggleThis">Blah blah + <p>
<div class="hiddenText">
<p>Blah blah</p>
</div>

css:

.hiddenText {display: none;}

Thanks!

+3  A: 

Instead of this:

 $(".hiddenText").slideToggle("fast");

Use this:

 $(this).next(".hiddenText").slideToggle("fast");

This approach finds it relatively to the clicked element (using .next()), rather than all class="hiddenText". If there are elements in-between that aren't in your example, use .nextAll(".hiddenText:first") instead.

Nick Craver
Thanks Nick, this works great. One quick follow up, if you don't mind... I'm getting a bit of an odd behavior with this. When there are multiple instances stacked one after another, when you expand and then collapse them, the collapse state (the paragraph) buts up right underneath the one above it. In other words, it seems to be violating the spacing set in my css. Not sure if the jQuery is stripping it for some reason. Any clues? Thanks again for your help.
@hollyb - What styling are you giving them in CSS? Is there supposed to be a bit of padding in-between?
Nick Craver
I have the following rules applied to the p:width: 530px; background-color:#f2f2f2; padding: 0 0 0 10px; height:20px; line-height:20px; text-transform:uppercase;Then, the hidden text is surrounded by a div with it's visibility hidded. Within that div is some text surrounded by another p which just inherits some normal padding. No matter what i do, when the div is contracted it butts right up to P above it.
I should add that when the divs are untoggled they are separated by 10px or so.
@hollyb - That style is on the `<p>` inside the `<div>` that's being slide-toggled, or is on the `<p class="toggleThis">`? If you had a link to a url, might be easier to see what's going on/
Nick Craver
@Nick Craver - OK, I figured out that the problem happens because of another peice of jQuery that this is nested in. I realize that I am doing this a bit inefficiently, but I can't figure out why my earlier function is causing this subtle problem. This probably goes beyond the call of duty, but if you're interested here is a mock-up: http://jsfiddle.net/BvVWd/4/. If you toggle it a couple times, you'll see what i mean.
@hollyb - I'm not seeing anything weird at all...which browser are you testing in?
Nick Craver
@Nick Craver - Looks like IE is the culprit. ARG!