Change div text with jQuery Toggle Change div text with jQuery Toggle javascript javascript

Change div text with jQuery Toggle


You can use the is() assertion method to check whether the panel is open or closed in the animation's callback and set the text accordingly - http://jsfiddle.net/9EFNK/7/

$('.open').click(function(){    var link = $(this);    $('.showpanel').slideToggle('slow', function() {        if ($(this).is(':visible')) {             link.text('close');                        } else {             link.text('open');                        }            });       });


Just add a simple if statement to test the text like so

$('.open').click(function(){       $('.showpanel').slideToggle('slow');       if($(this).text() == 'close'){           $(this).text('Show');       } else {           $(this).text('close');       }});

Like this DEMO


Not the prettiest of methods, but it does the job in a single statement.

$(this).text(($(this).text() == 'Close') ? 'Show' : 'Close');