html - jQuery: slideDown() showing wrong divs -
i have 4 different divs want show 4 different buttons. when uesr clicks specific link, specific div show up. html:
links:
<div class="col-md-3 text-center"> <a id="clickme1">i'm interested</a> </div> <div class="col-md-3 text-center"> <a id="clickme2">i'm interested</a> </div> <div class="col-md-3 text-center"> <a id="clickme3">i'm interested</a> </div> <div class="col-md-3 text-center"> <a id="clickme4">i'm interested</a> </div>
my divs:
<div class="col-md-3 text-center" id="ad1"> <p>show table here</p> </div> <div class="col-md-3 text-center" id="ad2"> <p>show table here</p> </div> <div class="col-md-3 text-center" id="ad3"> <p>show table here</p> </div> <div class="col-md-3 text-center" id="ad4"> <p>show table here</p> </div>
my jquery:
$("#clickme1").click(function () { if ($("#ad1").is(":hidden")) { $("#ad1").slidedown("slow"); } else { $("#ad1").hide(); } }); // ad 2 $("#clickme2").click(function () { if ($("#ad2").is(":hidden")) { $("#ad2").slidedown("slow"); } else { $("#ad2").hide(); } }); // ad 3 $("#clickme3").click(function () { if ($("#ad3").is(":hidden")) { $("#ad3").slidedown("slow"); } else { $("#ad3").hide(); } }); //ad 4 $("#clickme4").click(function () { if ($("#ad4").is(":hidden")) { $("#ad4").slidedown("slow"); } else { $("#ad4").hide(); } });
in css, 4 divs have attribute display:none.
problem
when click on links, 1 1, show fine. if click on other link start with, shows first div (#ad1). same when want hide them, if click on of them again, hides div. there must obvious step missing! please :(
instead of making id
s, rather use class. add data-slid-id
link, id should slide down or up.
when adding links , tables, don't need function.
$(function() { $('#ad1,#ad2,#ad3,#ad4').hide(); $('.clickme').on('click',function() { $('#'+$(this).data('slide-id')).slidetoggle('slow'); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-3 text-center"> <a class="clickme" data-slide-id="ad1">i'm interested</a> </div> <div class="col-md-3 text-center"> <a class="clickme" data-slide-id="ad2">i'm interested</a> </div> <div class="col-md-3 text-center"> <a class="clickme" data-slide-id="ad3">i'm interested</a> </div> <div class="col-md-3 text-center"> <a class="clickme" data-slide-id="ad4">i'm interested</a> </div> <div class="col-md-3 text-center" id="ad1"> <p>show table here 1</p> </div> <div class="col-md-3 text-center" id="ad2"> <p>show table here 2</p> </div> <div class="col-md-3 text-center" id="ad3"> <p>show table here 3</p> </div> <div class="col-md-3 text-center" id="ad4"> <p>show table here 4</p> </div>
Comments
Post a Comment