Reputation: 447
I feel I am very close to finishing this. I have a form with an "Add Children" button that when pressed will make a div show. The form has 15 divs hidden (#child1 - #child15). I have it working to show only the first row (#child1).
My problem is that when the button is pressed again, the next row (#child2, #child3, etc...) should appear and I am not sure how to get it to show. I tried putting in the counter but I am new to jQuery and any help would be very much appreciated. I don't expect this to be a difficult issue, just one that is eluding my novice ability.
Here is the jQuery. If i need to edit or add more code to help the question please let me know. Thanks in advance!
<script type="text/javascript">
var counter=0
$(document).ready(function() {
$('#addChildren').click(function() {
$('#child1').show(function() {
counter++
});
});
});
</script>
Upvotes: 0
Views: 202
Reputation: 5910
Add a class children-to-show
to your divs #child1 - #child15
and write
<script type="text/javascript">
$(document).ready(function() {
$('#addChildren').click(function() {
$('.children-to-show:hidden').first().show();
});
</script>
This way you dont't need a counter and the div id doesnt't have to be in a row, so you have more ways to configure your div ids.
Explanation
On every click on addChildren
, the next hidden element with class children-to-show
is shown.
EDIT
gdoron is right, so if you want to get it work with classes:
<script type="text/javascript">
$(document).ready(function() {
$('#addChildren').click(function() {
$('.children-to-show').not('.is-visible').first().show().addClass('is-visible');
});
</script>
Upvotes: 5
Reputation: 31141
You need to make the div you're showing a variable like $('#child' + counter)
so it will show a different div as the values of counter
get updated.
var counter=1
$(document).ready(function() {
$('#addChildren').click(function() {
$('#child' + counter).show(function() {
counter++
});
});
});
I've put together this demo to show you.
Upvotes: 1
Reputation: 61550
You could just concatenate the counter variable into the jQuery selector:
<script type="text/javascript">
var counter=0
$(document).ready(function() {
$('#addChildren').click(function() {
$('#child' + counter).show(function() {
counter++
});
});
});
</script>
Upvotes: 1
Reputation: 716
This should do it for you
<script type="text/javascript">
var counter=1
$(document).ready(function() {
$('#addChildren').click(function() {
$('#child' + counter).show(function() {
counter++
});
});
});
</script>
You can add a condition to check the limit of 15 as well. Something like ..
<script type="text/javascript">
var counter=1
$(document).ready(function() {
$('#addChildren').click(function() {
if(counter <= 15) {
$('#child' + counter).show(function() {
counter++
});
}
});
});
</script>
Upvotes: 1
Reputation: 150313
If the <div>
s are in order 1,2,3,4...15:
$('#addChildren').click(function() {
$('div[id^="child"]').eq(counter).show();
counter++;
});
If they are not in order:
$('#addChildren').click(function() {
$('#child' + (++counter)).show();
});
Upvotes: 2
Reputation: 13599
you can just use for loop if you know exact amount of divs
$(document).ready(function() {
$('#addChildren').click(function() {
for (i=1; i<=15; i++)
{
$('#child'+i).show();
}
});
});
Upvotes: 1