1

I am new for jquery, Here is my code:

<script type="text/javascript">
            $(document).ready(function () {
                $('.toAdd').hide();

                var count = 0;
                $('#add').on('click', function () {
                    $('.toAdd:eq(' + count + ')').show();
                    count++;
                });
            });

</script>
<div class="toAdd">One</div><div class="toAdd">Two</div><div class="toAdd">Three</div>
<input type="button" value="show" id="add"/>
<input type="button" value="hide" id="sub"/>

In this code if i click show button the divisions one by one it showing. after that i need to hide one by one if i click hide button

Fiddle here

2
  • 1
    And what have you tried already? Commented Oct 11, 2013 at 6:10
  • $(document).ready(function () { $('.toAdd').show(); var count > 0; $('#sub').on('click', function () { $('.toAdd:eq(' + count + ')').hide(); count--; }); }); like that i tried but its not working Commented Oct 11, 2013 at 6:11

7 Answers 7

5
$(document).ready(function () {
    $('.toAdd').hide();

    var count = 0;
    $('#add').on('click', function () {
        $('.toAdd:eq(' + count + ')').show();
        count++;
    });
    var deCount = count;
    $('#sub').on('click', function () {
        count--;
        $('.toAdd:eq(' + count + ')').hide();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

4

Hope this might help you... :)

                 $('#sub').on('click', function () {
                        if(count > 0){
                           count--;
                        $('.toAdd:eq(' + count + ')').hide();
                        }

                    });

2 Comments

<script>$(document).ready(function () { $('.toAdd').hide(); var count = 0; $('#add').on('click', function () { $('.toAdd:eq(' + count + ')').show(); count++; }); $('#sub').on('click', function () { count--; $('.toAdd:eq(' + count + ')').hide(); }); }); </script>
please do check the value of the counts.I have edited the code,so that the count variable is decremented only if the value is greater than 0
2

Edited jsfiddle by Outlooker

if($('.toAdd:eq(' + count + ')').is('*'))

Added check of item existance.

Fiddle

Comments

2

Try this too

<script type="text/javascript">
            $(document).ready(function () {
                $('.toAdd').hide();

                $('#add').on('click', function () {
                    $('.toAdd,.hidden').first().show().addClass( "shown" ).removeClass( "hidden" );
                });
                $('#sub').on('click', function () {
                    $('.toAdd,.shown').last().hide().addClass( "hidden" ).removeClass( "shown" );
                });
            });

2 Comments

Are you aware of jquery chaining
@gowri I wasn't. But now I am. Thanks
2

The answer of Outlooker is right but have some little bit error. when user will click on show button 4th time then your hide doesn't work as expected. So I just fix the chunk here and sharing with you guys.

Html Code:

<div class="toAdd">One</div>
<div class="toAdd">Two</div>
<div class="toAdd">Three</div>
<input type="button" value="show" id="add" />
<input type="button" value="hide" id="sub" />

Java Script Code:

/**
 * Hide all Content div
 */
$(".toAdd").hide();
/**
 * Total no of content div find out
 **/
var lengthDiv = $(".toAdd").length;
/**
 * Default count declare
 **/
var count = 0;
/**
 * Click on show button
 **/
$('#add').on('click', function () {
    if (count < lengthDiv) {
        $('.toAdd:eq(' + count + ')').show();
        count++;
    }
});
/**
 * Click on hide button
 **/
$('#sub').on('click', function () {
    if (count > 0) {
        count--;
        $('.toAdd:eq(' + count + ')').hide();
    }
});

Fiddle Example

Comments

1

Try this this will work you very fine

    $('.toAdd').hide();
    $('#add').click(function(){
    $('div').each(function(key, value) {

            $(value).delay(key * 500).fadeIn(500);

        });});
        $('#sub').click(function(){
        $('div').each(function(key, value) {

            $(value).delay(key * 500).fadeOut(500);

        });
});

Fiddle Here

Comments

1

Try

var $toAdds = $('.toAdd').hide();

var count = 0;
$('#add').on('click', function () {
    if (count < $toAdds.length) {
        $toAdds.eq(count).show();
        count++;
    }
});
$('#sub').on('click', function () {
    if (count > 0) {
        count--;
        $toAdds.eq(count).hide();
    }    
});

Demo: Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.