Reputation: 25
I am trying to create a test page
For the last block to hide the button
My code:
$("#test_inner .test-item").each(function(e) {
if (e != 0)
$(this).hide();
});
$(".next-btn").click(function(){
if ($("#test_inner .test-item:visible").next().length != 0)
$("#test_inner .test-item:visible").next().show().prev().hide();
else {
$("#test_inner .test-item:visible").hide();
$("#test_inner .test-item:first").show();
}
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="test_inner">
<div class="test-item">DIV1</div>
<div class="test-item">DIV2</div>
<div class="test-item">DIV3</div>
<div class="test-item">DIV4</div>
<div class="test-item">DIV5</div>
<a href="#" class="next-btn">send</a>
</div>
I would be grateful for any help.
Upvotes: 0
Views: 52
Reputation: 694
$("#test_inner .test-item").each(function(e) {
if (e != 0)
$(this).hide();
});
var array = $(".test-item");
var index = 0;
$(".next-btn").click(function(){
if(index==array.length-1){
$(this).hide();
}
array.eq(index++).hide();
array.eq(index).show();
})
Upvotes: 1
Reputation: 7065
You can set a counter and keep a check on last element. If last element is reached hide the button.
Here is the updated code:
var i = 0;
$(".next-btn").click(function(){
i++;
if(i == 4)
{
$(this).hide();
}
if ($("#test_inner .test-item:visible").next().length != 0)
$("#test_inner .test-item:visible").next().show().prev().hide();
else {
$("#test_inner .test-item:visible").hide();
$("#test_inner .test-item:first").show();
}
return false;
});
Upvotes: 2