1
<button class="btn hide_show">Don't show text</button>

$(".hide_show").toggle(function () {
    $(this).text("Show text");
}, function () {
    $(this).text("Don't show text");
});

http://jsfiddle.net/x83qg/1/

I don't know exactly why this code doesn't work. The idea is toggle the text of the button

5
  • .toggle() isn't an event listener but a function. You should replace .toggle() by .click(). Then, make a test on the displayed text. If text, hide it, else, display it. Commented Dec 5, 2013 at 11:29
  • 1
    If you're having trouble with a particular jQuery function why not look at the documentation for that function to see if you're using it correctly? Commented Dec 5, 2013 at 11:30
  • Your code is working just fine! See this update: jsfiddle.net/x83qg/6 . Problem however is because you use toggle. If hidden, how would you see the changed text? Commented Dec 5, 2013 at 11:32
  • 1
    @abhitalks - The code certainly isn't working fine given that it doesn't do what the OP wants. The button's visibility isn't supposed to be toggled - the OP is trying to use the other .toggle() method. Commented Dec 5, 2013 at 11:42
  • @nnnnnn: That is exactly what I was trying to convey. Op wanted to change text which is actually happening. However, the problem is in toggle! He was unable to see because toggle was hiding it. Commented Dec 5, 2013 at 11:46

3 Answers 3

2

The toggle function you have executes on DOM ready and hides the button and there is no way to show it again. You probably need to change the text of click.

Live Demo

$(".hide_show").click(function () {
   $(this).text( $(this).text() == "Show text" ? "Don't show text" :"Show text");
});
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery originally had two .toggle() functions. One toggles visibility, and the other sets up a click handler that toggles between multiple handlers. Which function was used depended on what arguments were passed in. It seems to be the latter that you are trying to use, but it was deprecated in v1.8 and removed in v1.9.

Fortunately it's pretty easy to implement something like the removed .toggle():

var text = ["Show text", "Don't show text"],
    i = 0;
$(".hide_show").click(function () {
    $(this).text(text[i]);
    i = (i + 1) % text.length;
});

Demo: http://jsfiddle.net/x83qg/12/

Note that (like the old .toggle() function that allowed more than two handlers) the code I've shown will cycle through all items in the array, so you're not restricted to just switching back and forth between only two items.

Comments

0

Try:

var txt = $(".hide_show").text().toLowerCase();
if(txt == "show text"){
    $(".hide_show").text("Don't show text");
}
else{
    $(".hide_show").text("Show text");
}

DEMO here

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.