I have a problem with this code: if you click the "appened text" twice you will get 2 divs one with content of "hello2" and one with "hello3", but for some resone when you click on them they open the same block. insted of having each of them opening his own block.(I want to keep it dynamically created.) thanks for the help
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
var k=1;
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
function appendText()
{
k=k+1;
var css = '#flip'+k+' { padding:5px; text-align:center; background-color:#ffff00; border:solid 1px #c3c3c3;}',
head = document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
var css = '#panel'+k+'{padding:50px;display:none;text-align:center; background- color:#'+k+''+k+''+k+''+k+''+k+''+k+'; border:solid 1px #c3c3c3;}',
head = document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
var flip2 = document.createElement("div");
flip2.innerHTML = "Hello"+k;
flip2.id = ("flip"+k);
var panel2= document.createElement("div");
panel2.innerHTML = "Hello"+k;
panel2.id = ("panel"+k);
document.body.appendChild(flip2);
document.body.appendChild(panel2);
$(document).ready(function(){
$(("#flip"+k)).click(function(){
$(("#panel"+k)).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>
<p>This is a paragraph.</p>
<button onclick="appendText()">Append text</button>
<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>
</body>
</html>
k?