Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Matthew Smart
Matthew Smart
12,567 Points

How to apply the fadeToggle() to only toggle the selected class.(i have multiple class's with the same name)

i have 3 .slot classes. inside the .slot contains .top .linkbar and .linktext.

When i click on the first .slot .top i want the first .linktext to appear underneath.

With my code so far. when i click on the first .slot .top it shows all of 3 of the .linktext.

i want it to work depending on which .slot .top i click, meaning if i click the bottom .slot .top it will show the bottom .linktext.

CSS: .slot{ margin-bottom: 30px; width: 600px; height: inherit; box-shadow: 0px 0px 10px #e7a61b; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 14px; border-top-right-radius: 14px; border: 1px solid #7e7b75; float: left;

    }
    .slot:hover{
        box-shadow: 0px 0px 25px #e7a61b;
    }

    .top{
        width: 600px;
        height: 171px;
        border-top-left-radius: 14px;
        border-top-right-radius: 14px;
        float: left;
    }

    .linkbar{
        width: 600px;
        height: 37px;
        background-image: url('../img/linkbar2.gif');
        float: left;
    }


    .linktext{
        width: 600px;
        height: inherit;
        border-bottom-left-radius: 14px;
        border-bottom-right-radius: 14px;
        float: left;
        display: none;
     }

HTML <div class="slot"> <div class="top"> <?php echo $this->html->image('carpark.gif');?> </div> <div class="linkbar"> <h3>Car Parks</h3> </div> <div class="linktext"> <h4>-hello</h4> <h4>-hello</h4> <h4>-hello</h4> </div> </div><!-- END OF SLOT -->

                            <div class="slot">
                                            <div class="top">
                                                <?php echo $this->html->image('road.gif');?>
                                            </div>

                                            <div class="linkbar">
                                                <h3>Road Markings</h3>
                                            </div> 
                                            <div class="linktext">
                                                <h4>-yo</h4>
                                                <h4>-hello</h4>
                                                <h4>-hello</h4>
                                            </div>
                           </div><!-- END OF SLOT -->



                            <div class="slot">
                                            <div class="top">
                                                <?php echo $this->html->image('sports.gif');?>
                                            </div>

                                            <div class="linkbar">
                                                <h3>Sports Court</h3>
                                            </div> 
                                            <div class="linktext">
                                                <h4>-hello</h4>
                                                <h4>-hello</h4>
                                                <h4>-yo</h4>
                                            </div>
                           </div><!-- END OF SLOT -->

Jquery:

                 $('document').ready(function () {
                    $('.slot').hide();
                    $('.linktext').hide();
                    $('body').fadeIn(2000);
                    $('.slot').slideDown(2000);

                });







                $('.top').click(function () {

                    $('.linktext').fadeToggle('slow');

                });

1 Answer

Aaron Graham
Aaron Graham
18,033 Points

You might try something like this:

$('.top').click(function () {
  $(this).siblings('.linktext').fadeToggle('slow');
});

The way you have it, $('.linktext') is selecting all the elements with that class. You can narrow your selection using $(this) to only elements that are somehow related to the element receiving the click event.