javascript - Show and Hide functions using Jquery - Stack Overflow

I am new for jquery, Here is my code:<script type="textjavascript">$(document).ready(f

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

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

Share Improve this question edited Oct 11, 2013 at 6:12 S. S. Rawat 6,1314 gold badges44 silver badges59 bronze badges asked Oct 11, 2013 at 6:07 user2869777user2869777 159 bronze badges 2
  • 1 And what have you tried already? – u_mulder 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 – user2869777 Commented Oct 11, 2013 at 6:11
Add a ment  | 

7 Answers 7

Reset to default 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();
    });
});

Hope this might help you... :)

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

                    });

Edited jsfiddle by Outlooker

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

Added check of item existance.

Fiddle

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" );
                });
            });

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742297646a4417445.html

相关推荐

  • javascript - Show and Hide functions using Jquery - Stack Overflow

    I am new for jquery, Here is my code:<script type="textjavascript">$(document).ready(f

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信