1

I am looking to create my first web based jQuery app. I have acheived half of the result that I want. Updating my button to change to 'Hide Answer' once clicked, however I would like the button to change back to the original text of 'Show Answer' when the user clicks an activates the toggle...

$(".answer-button").click(function(){
   $(this).html("Hide Answer").prev().toggle();
});

jsfiddle included here;

https://jsfiddle.net/rossmclenny/51jg4f79/1/

1
  • 1
    Why use a jsfiddle for this? Just use stackoverflow's builtin feature. Commented Nov 5, 2020 at 15:49

2 Answers 2

1

you can test the visible property

$(".answer-button").click(function(){
    $(this).prev().toggle();
    if($(this).parent().find('#answer').is(':visible')){
        $(this).html("Hide Answer");
    }else{
       $(this).html("Show Answer");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

just do that by testing the text value:

    $(".answer-button").click(function () {
      if ($(this).text().includes("Show")) {
        $(this).html("Hide Answer").prev().toggle();
      } else {
        $(this).html("Show Answer").prev().toggle();
      }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.