When the page loaded,I want to display only two comments at first.But when i clicked on the button ,i want to increase the display number of comments by two.But nothing happen as my wish.I have tried but my code is not working.It work reverse as i want.I display all the comments when page loaded and display only two comments when i click on button.Any ideas how i can fix this bugs.Thanks is advance...
Here is demo code...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
.d-none {
display:none;
}
</style>
<body>
<div class="post">
<div class="post-comment">
<button class="view_next_comment">View Only Limited</button>
<h1>This is comment one.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
</div>
<div class="post">
<div class="post-comment">
<button class="view_next_comment">View Only Limited</button>
<h1>This is comment one.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
<div class="post-comment">
<h1>This is comment two.</h1>
</div>
<div class="post-comment">
<h1>This is comment three.</h1>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
let start1=0;
let limit = 2;
function show_few_comments(start1,limit){
$('.post').each(function(){
var comments = $(this).find('.post-comment');
var comments_length = comments.length;
//alert(limit)
if( comments_length > limit ){
for( var start1=0; start1<comments_length; start1++ ){
if(start1>=limit){
$(comments[start1]).addClass('d-none');
}
}
}
})
}
$(document).on('click','.view_next_comment',function(){
var start1 =start1+limit;
show_few_comments(start1,limit);
})
</script>
</body>
</html>
start1with an inner one because you have an inner declaration (var start1 =start1+limit;). If you want to usestart1in that callback, use a different name for the local variable. If you want to use the outerstart1, remove thevar.