Reputation: 631
This is my code :
<script type="text/javascript">
$(document).ready(function(){
$('.toAdd').hide();
$('#add').click(function () {
var i = 0;
$('.toAdd').each(function () {
if ($(this).show()) {
i++;
}
});
});
});
</script>
<div id=add><input type="button" value="click"/>
</div>
<div id="toAdd">
<label>1</label>
</div>
<div id="toAdd">
<label>2</label>
</div>
<div id="toAdd">
<label>3</label>
</div>
<div id="toAdd">
<label>4</label>
</div>
In this code i need to show div one by one for each click event but its not working?
Upvotes: 0
Views: 4556
Reputation: 36531
change id to class, like other have said and if you need to cycle the show/hide. then this might come handy
$('.toAdd').hide();
var i = 0;
$('#add input').click(function () {
i=(i >= 4)?0:i;
//or if(i >= 4){ i=0;}
$('.toAdd').eq(i).show().siblings().not(':first').hide();
i++;
});
Upvotes: 0
Reputation: 781120
As others have said, you need to change id="toAdd"
to `class="toAdd".
You can then show the first unhidden DIV with:
$('#add input').click(function () {
$('.toAdd:hidden:first').show();
});
Upvotes: 2
Reputation: 20834
ID must be unique, use classes instead.
$('.toAdd').hide();
var count = 0;
$('input').on('click',function(){
$('.toAdd:eq('+count+')').show();
count++;
});
Upvotes: 6
Reputation: 6000
Replace <div id="toAdd"> with <div class="toAdd">
and do this
$('.toAdd').each(function () {
if (!$(this.is(":visible"))) {
$(this).show();
break;
}
});
Upvotes: 0