5

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:

<input type="button" class="reveal" onClick="this.value=this.value=='Show Answer'?'Hide Answer':'Show Answer'" value="Show Answer">

However, it does not work. How should I implement this? Any help would be great.

1
  • Why are you showing an input in your question, but using button elements in your linked demo? Commented Jan 19, 2014 at 10:06

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

6 Comments

should I change this as well $("a[href='" + window.location.hash + "']").parent(".answerbutton").click(); not working in wordpress but it is fine in jsfiddle.
In the jsfiddle attached you don't have any a tag. What does it suppose to do?
OK, you want the question to be open by the href and location?
it supposed to be window.location.href (for all url) or window.location.href.hash (only for the hash)
use: $(this).parent().next('.toggle_container').slideToggle("fast");
|
6

Very easy to change the text using jQuery.

Working example here. http://jsfiddle.net/vp7Y7/

$('.reveal').click(function() {
    if ($(this).text() === 'Show Answer') {
         $(this).text('This is NEW TEXT!!! It Worked!');
    }
    else {
        $(this).text('Show Answer');
    }
});

2 Comments

Thanks, it really worked, but when I click it again, it does not revert to Show answer text.
Thanks, still I am unable to get it :( sorry for being naive. Please see this fiddle, only third button is working not the first two ones.jsfiddle.net/cED6c/10
1

something easy like this

 $(document).ready(function () {
    $('a.edit').click(function () {
        var txt = $(this).text();
        txt = (txt !='cancel') ? 'cancel' : 'edit';
        $(this).text(txt);
        $('.editForm').toggle(300);
     });
});

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.