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(); }); });
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.
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.
$(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.
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