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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With