I would like to thanks to this post enter link description here it is actually working but only when html document written directly.
But it doesn’t work while apply the same thing in document that created by jquery. can you please advise how to solve this?
my code is
<script>
var toggle = false;
$(function() {
$('a.comments').click(function(e) {
var $this = $(this);
$('.toggleComments').toggle(1000,function() {
if(!toggle) {
$this.text('Hide Comments');
toggle = !toggle;
}else {
$this.text('Show Comments');
toggle = !toggle;
}
});
e.preventDefault();
});
});
var html;
$(document).ready(function()
{
html='<a href="#" class="comments">Show Comments</a><br /><div class="toggleComments">This is #comment1 <br />This is #comment2 <br /></div>';
$("#start").append(html);
});
</script>
</head>
<body>
<div id="start"></div>
</body>
</html>
To have your event handler applied to dynamically created elements, you need the on function :
The provided function will be called on click even on elements added to the DOM in the future.