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
$(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.
this is not the very clean and smart way but its very easy to understand and use somtimes - its like odd and even - boolean like:
var moreOrLess = 2;
$('.Btn').on('click',function(){
if(moreOrLess % 2 == 0){
$(this).text('text1');
moreOrLess ++ ;
}else{
$(this).text('more');
moreOrLess ++ ;
}
});