lucafj2j282j
lucafj2j282j

Reputation: 891

How do I use my jQuery slidetoggle more than once?

I have my jQuery slidetoggle and it works for the first <div> but when adding more flip's and panel's it won't work. It has to be like this. I click on the one above and it works. The second one doesn't even react. How do I fix that?

The full code:

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

<style> 
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}

#panel {
padding: 50px;
display: none;
}

#panel1, #flip1 {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}

#panel1 {
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>

<br><br>

<h1 style="text-align: center;">This example below won't work. Why not?</h1><br>    <br>
<div id="flip">Click to slide the seconds panel down or up</div>
<div id="panel">Hello world!</div>

<br><br>

<h1 style="text-align: center;">To fix this I could try using different names for the flip and panel such as flip1 and panel1</h1><br><br>


<div id="flip1">Click to slide the panel down or up</div>
<div id="panel1">Hello world!</div>
</body>
</html>

Upvotes: 1

Views: 191

Answers (1)

user407283
user407283

Reputation:

ID must be unique here is the demo

$(document).ready(function(){
    $("#flip").click(function(){
        $("#panel").slideToggle("slow");
    });
    $("#flip1").click(function(){
        $("#panel1").slideToggle("slow");
    });
});

please use jsfiddle as below better version :)

Upvotes: 1

Related Questions