Vikash Shandilya
Vikash Shandilya

Reputation: 21

How can i use jquery slide toggle with class?

I have used following code from http://w3schools.com. But in this I can use only once. I want to use it more than one times because I want to create FAQ panel in HTML but it is from ID and I can use it only once.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script> 
$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});
</script>

<style type="text/css"> 
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>

<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>

</body>
</html>

Upvotes: 2

Views: 8446

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

change the id to class and then use class selectors in css and jQuery

<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>

CSS

.panel, .flip {
    padding:5px;
    text-align:center;
    background-color:#e5eecc;
    border:solid 1px #c3c3c3;
}
.panel {
    padding:50px;
    display:none;
}

then

$(document).ready(function () {
    $(".flip").click(function () {
        $(this).next('.panel').slideToggle("slow");
    });
});

Demo: Fiddle

Upvotes: 3

user2663434
user2663434

Reputation:

demo

script:

$(function(){
    $('a.toggle').click(function(){
        $('.myContent').stop().slideToggle(500);
        return false;
    });

});

HTML

<a href="#" style="background-color: #99CCFF; padding: 5px 10px;" class="toggle">CLICK</a>

<div>
    <div class="myContent" style="background-color: #99CCFF; padding: 5px 10px;">Optimized the javascript so that all code is based on jQuery.
    </div>

    <div class="myContent" style="background-color: #CCCCFF; padding: 5px 10px;">Optimized the javascript so that all code is based on jQuery.
    </div>
</div>

Upvotes: 6

Related Questions