Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Toggle Text?

Tags:

jquery

toggle

How to toggle HTML text of an anchor tag using jQuery? I want an anchor that when clicked the text alternates between Show Background & Show Text as well as fading in & out another div. This was my best guess:

$(function() {     $("#show-background").click(function () {         $("#content-area").animate({opacity: 'toggle'}, 'slow');      });      $("#show-background").toggle(function (){         $(this).text("Show Background")         .stop();     }, function(){         $(this).text("Show Text")         .stop();     }); }); 
like image 251
mtwallet Avatar asked Jan 28 '10 15:01

mtwallet


People also ask

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.

What is the use of toggle () method in jQuery?

jQuery toggle() Method The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on.


Video Answer


1 Answers

$(function() {     $("#show-background").click(function () {         $("#content-area").animate({opacity: 'toggle'}, 'slow');      });      var text = $('#show-background').text();     $('#show-background').text(         text == "Show Background" ? "Show Text" : "Show Background"); }); 

Toggle hides or shows elements. You could achieve the same effect using toggle by having 2 links and toggling them when either is clicked.

like image 116
Kyle Butt Avatar answered Oct 04 '22 09:10

Kyle Butt