Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button text toggle in jquery

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.

like image 997
Dorukcan Kişin Avatar asked Nov 30 '12 20:11

Dorukcan Kişin


People also ask

How do you toggle text on a button click?

To toggle a button's text on click:Add a click event listener to the button. Each time the button is clicked, check if the button's text is the initial text. If it is, change the text to the clicked text.

How do I toggle a button in jQuery?

The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

How do you add text on toggle switch?

Toggle Switch Text You can display additional text with the toggle switch by adding the toggle-switch-text class to the text element. Use the toggle-switch-text-left and toggle-switch-text-right classes to position the text on the left and right side of the toggle switch, respectively.


1 Answers

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/

like image 163
undefined Avatar answered Oct 01 '22 06:10

undefined