Change div text with jQuery Toggle

后端 未结 8 783
粉色の甜心
粉色の甜心 2021-02-01 04:07

When using slideToggle, how to change the Text close/show? I did a simple one, but cannot get the text change back.

Here is what I did:

相关标签:
8条回答
  • 2021-02-01 04:58

    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

    0 讨论(0)
  • 2021-02-01 05:03

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

    $(this).text(($(this).text() == 'Close') ? 'Show' : 'Close');
    
    0 讨论(0)
提交回复
热议问题