Button text toggle in jquery

JqueryButtonToggle

Jquery Problem Overview


When i click ".pushme" button, it turns its text to "Don't push me". I want to turn the text again to "push me" when button is clicked again. How can i do that?

<html>

<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>
	<button class='pushme'>PUSH ME</button>
	<script>
		$(".pushme").click(function () {
			$(this).text("DON'T PUSH ME");
		});
	</script>
</body>

</html>

http://jsfiddle.net/DQK4v/

Thanks.

Jquery Solutions


Solution 1 - Jquery

You can use text method:

$(function(){
   $(".pushme").click(function () {
      $(this).text(function(i, text){
          return text === "PUSH ME" ? "DON'T PUSH ME" : "PUSH ME";
      })
   });
})

http://jsfiddle.net/CBajb/

Solution 2 - Jquery

You could also use .toggle() like so:

$(".pushme").toggle(function() {
    $(this).text("DON'T PUSH ME");
}, function() {
    $(this).text("PUSH ME");
});

More info at http://api.jquery.com/toggle-event/.

This way also makes it pretty easy to change the text or add more than just 2 differing states.

Solution 3 - Jquery

Use a custom ID if possible if you would like to apply the action to only that button.

###HTML

<button class="pushDontpush">PUSH ME</button>

###jQuery

$("#pushDontpush").click(function() { 
    if ($(this).text() == "PUSH ME") { 
        $(this).text("DON'T PUSH ME"); 
    } else { 
        $(this).text("PUSH ME"); 
    }; 
});

Working CodePen: Toggle text in button

Solution 4 - Jquery

Use an if/else statement.. or ternary if you understand it

$(".pushme").click(function () {
    var $el = $(this);
    $el.text($el.text() == "DON'T PUSH ME" ? "PUSH ME": "DON'T PUSH ME");
});

http://jsfiddle.net/dFZyv/

Solution 5 - Jquery

With so many great answers, I thought I would toss one more into the mix. This one, unlike the others, would permit you to cycle through any number of messages with ease:

var index = 0,
    messg = [
        "PUSH ME", 
        "DON'T PUSH ME", 
        "I'M SO CONFUSED!"
    ];

$(".pushme").on("click", function() {
    $(this).text(function(index, text){
        index = $.inArray(text, messg);
        return messg[++index % messg.length];
    });
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​);​

Demo: http://jsfiddle.net/DQK4v/2/

Solution 6 - Jquery

$(".pushme").click(function () {
  var button = $(this);
  button.text(button.text() == "PUSH ME" ? "DON'T PUSH ME" : "PUSH ME")           
});

This ternary operator has an implicit return. If the expression before ? is true it returns "DON'T PUSH ME", else returns "PUSH ME"

This if-else statement:

if (condition) { return A }
else { return B }

has the equivalent ternary expression:

condition ? A : B

Solution 7 - Jquery

I preffer the following way, it can be used by any button.

<button class='pushme' data-default-text="PUSH ME" data-new-text="DON'T PUSH ME">PUSH ME</button>

$(".pushme").click(function () {
    var $element = $(this);
    $element.text(function(i, text) {
        return text == $element.data('default-text') ? $element.data('new-text')
                                                     : $element.data('default-text');
    });
});

demo

Solution 8 - Jquery

If you're setting the button text by using the 'value' attribute you'll need to set

  • $(this).val()

instead of:

  • $(this).text()

Also in my situation it worked better to add the JQuery direct to the onclick event of the button:

onclick="$(this).val(function (i, text) { return text == 'PUSH ME' ? 'DON'T PUSH ME' : 'PUSH ME'; });"

Solution 9 - Jquery

You can also use math function to do this

var i = 0;
$('#button').on('click', function() {
    if (i++ % 2 == 0) {
        $(this).val("Push me!");
    } else {
        $(this).val("Don't push me!");
    }
});

DEMO

Solution 10 - Jquery

Instead of incrementing the above example to infinity, you can use a simpler solution:

var i=0;
      $('#button').on('click', function() {
          if(i == 0){
          	$(this).val("Don't push me!"); i = 1;
          }else{
            $(this).val("Push me!"); i = 0;
          }
      });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDorukcan KişinView Question on Stackoverflow
Solution 1 - JqueryRamView Answer on Stackoverflow
Solution 2 - JquerygotohalesView Answer on Stackoverflow
Solution 3 - Jquerytimbck2View Answer on Stackoverflow
Solution 4 - Jquerywirey00View Answer on Stackoverflow
Solution 5 - JquerySampsonView Answer on Stackoverflow
Solution 6 - JqueryArt KnipeView Answer on Stackoverflow
Solution 7 - JqueryRicardo Alvaro LohmannView Answer on Stackoverflow
Solution 8 - JqueryChris HalcrowView Answer on Stackoverflow
Solution 9 - JqueryJSEvgenyView Answer on Stackoverflow
Solution 10 - Jqueryuser1286956View Answer on Stackoverflow