Skip to content

Instantly share code, notes, and snippets.

@danieldunderfelt
Created January 17, 2014 11:52
Show Gist options
  • Save danieldunderfelt/8472218 to your computer and use it in GitHub Desktop.
Save danieldunderfelt/8472218 to your computer and use it in GitHub Desktop.
Quick and dirty jQuery plugin to toggle an element's text between two alternatives.
/*
* Author: Daniel Dunderfelt
*
* Use: $(element).toggleText("text alternative 1", "text alternative 2");
*/
(function($) {
$.fn.extend({
toggleText: function(toggle1, toggle2) {
return this.each(function(i, ele) {
var currentText = $(ele).text();
if(currentText === toggle1) {
$(ele).text(toggle2);
}
else {
$(ele).text(toggle1);
}
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment