Search code examples
jquerytogglebutton

Jquery plus and minus button toggle


I am a bit of an amateur so I don't know how to achieve this goal. I would have some buttons that need to toggle plus or minus:

http://jsfiddle.net/tonylka10/0eq456w9/

Jquery:

$(document).ready(function () {
$('.expandable').click(function () {
    $('.expandable').not(this).nextAll('tr').hide();
    $(this).nextAll('tr').toggle(350);

});
$('.expandable').nextAll('tr').each(function () {
    if (!($(this).is('.expandable'))) $(this).hide();
});
});

How can I achieve this with the code that I already have?

I would like to have the same functionality as the example below:

http://jsfiddle.net/v9Evw/1/

Any help would be appreciated, thank you in advance.


Solution

  • Working fiddle :

    http://jsfiddle.net/0eq456w9/10/

    $('.expandable').click(function () {
        $('.expandable').not(this).nextAll('tr').hide();
        $('.expandable').not(this).find('input[type="button"]').val("+");
        $(this).nextAll('tr').toggle(350);
        if( $(this).find('input[type="button"]').val() == "+")
        {
            $(this).find('input[type="button"]').val("-");
        }
        else
        {
            $(this).find('input[type="button"]').val("+");
        }
    
    });