Toggle text inside div

Marko Krstić
29 Nov 2019

jQuery Snipped for toggle text inside div on click.

“Hello World” will be default text and text for every second click.
And “Bye!” will be on first and every even click.

jQuery.fn.extend({
    toggleText: function (a, b){
        var isClicked = false;
        var that = this;
        this.click(function (){
            if (isClicked) { that.text(a); isClicked = false; }
            else { that.text(b); isClicked = true; }
        });
        return this;
    }
});
jQuery('.toggle-text').toggleText("Hello World", "Bye!");

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy