Apply a function to only one div class, not all

I am currently working on a location page on a website and am having problems with the show / hide jquery effect. The jQuery below is activated when clicked and is used to switch the div class to show / hide it ... quite simply. However, when I add more divs with the same class and click on the activator link, it launches functions in all divs. I know this is a simple fix, but I cannot figure out the correct syntax. Can anyone take a quick look at this code and see how I can fix it. I have included only one of the html sections below. This is when I have more than one, which causes a problem. See an example here .

Thanks Taylor

JQuery

$(function(){
    $(".sliding").hide();
    $(".show-hide").show();
    $(".hide-click").show();

    $('.show-hide').click(function(){   
        $(".sliding").slideToggle("slow");
        $(".hide-click").slideToggle("slow");
    });
});

HTML

<!-- Bowie, MD -->
<div class="location" style="margin-top: 0;">

<!-- City Name -->
<h3><a href="javascript:void(0);" class="show-hide">Bowie</a></h3>

    <h2>Address</h2>
    <p>16901 Melford Blvd, Suite 11<br/>
    Bowie, MD 20715</p>

    <h2>Contact</h2>
    <p><span><strong>Phone:</strong> 301.805.5395</span><br />
    <span><strong>Fax:</strong> 301.805.5396</span></p>

    <span class="hide-click"><a href="javascript:void(0);" class="show-hide">View additional info</a></span>

    <!-- Hidden Content -->
    <div class="sliding">

        <h2>Map</h2>

        <div class="map">   
            <iframe width="211" height="140" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&amp;sll=37.0625,-95.677068&amp;sspn=61.023673,112.236328&amp;ie=UTF8&amp;hq=&amp;hnear=16901+Melford+Blvd,+Bowie,+Maryland+20715&amp;ll=38.959742,-76.714354&amp;spn=0.009344,0.018196&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>
        </div>

        <a href="http://maps.google.com/maps?f=d&source=s_d&saddr=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&daddr=&hl=en&geocode=&mra=prev&sll=38.959741,-76.714349&sspn=0.00755,0.013422&g=16901+Melford+Blvd,+Suite+11+Bowie,+MD+20715&ie=UTF8&z=17" target="_blank" class="directions">Get Directions</a>

        <h2>Doctors</h2>
        <p><a href="/#phillips.php">William B. Phillips, II M.D.</a>, <a href="/#weichel.php">Eric D. Weichel, M.D.</a></p>
        <a href="javascript:void(0);" class="show-hide">Hide additional info</a>

    </div>
    <!-- end Hidden Content -->

</div>
<!-- end Location -->
+3
7

, .sliding, .show-hide . jQuery .sliding. , , , css, .

$(function(){
    $(".sliding").hide();
    $(".show-hide").show();
    $(".hide-click").show();

    $('.show-hide').click(function(){   
        //find the surrounding div
        var location = $(this).parent().parent();

        //find the correct elements within the location block
        $(".sliding",location).slideToggle("slow");
        $(".hide-click",location).slideToggle("slow");
    });
});
+4
+3

div a id, #divID jQuery, div:

$('#divID').hide();

, , .

+1

:

1) id $() id div, ,

<div id="someid">, js $("#someid").show()

2) $($(). get (N)).show(), N - div, . , , , .

+1

div

  $(this).parents(".sliding").slideToggle("slow")
+1

.

div, , dn, display:none css.

javascript - . , , .

$(function(){

    $('.show-hide').click(function(){

        $(".sliding").slideUp("slow");
        $(this).next(".sliding).slideDown("slow");

   });
});
+1

:

$(".sliding:eq(0)").slideToggle("slow");  

sets it to the first element of the ".sliding" class.

See: How to get the nth jQuery element

0
source

All Articles