Change button text (show / hide/ show) on click in jquery toggle

前端 未结 3 1183
情书的邮戳
情书的邮戳 2021-02-10 06:27

I am working on this fiddle: http://jsfiddle.net/cED6c/7/ I want to make the button text change on click and I tried using the following code:



        
3条回答
  •  粉色の甜心
    2021-02-10 07:02

    You need to add this code:

    if ($.trim($(this).text()) === 'Show Answer') {
        $(this).text('Hide Answer');
    } else {
        $(this).text('Show Answer');        
    }
    

    You can see it at work:

    http://jsfiddle.net/cED6c/13/

    I add the trim function cause in the html you got space after the Show Answer.

提交回复
热议问题