JSFiddle - React, Tailwind, and code Playground
by witq
HTML
<textarea rows="10" cols="20" class="case"></textarea>
JavaScript
;(function ($, window, document, undefined) {
// Create the defaults once
var pluginName = "silence",
defaults = {
// eventual options here
};
// The actual plugin constructor
function Silence(element, options) {
this.element = element;
this.$element = $(element);
this.settings = $.extend({}, defaults, options);
this.init();
}
Silence.prototype = {
init: function () {
var that = this;
this.$element.on('keyup', function () {
that.transform();
});
},
transform: function () {
var text = this.$element.val(),
textArray;
textArray = text.split(' ');
for (var i = 0; i < textArray.length; i++) {
if (textArray[i].length > 1 && textArray[i].match(/^[A-Z]*$/)) {
textArray[i] = textArray[i].toLowerCase();
} else {
var trans = textArray[i].substr(1).toLowerCase(),
noTrans = textArray[i].substr(0, 1);
textArray[i] = noTrans + trans;
}
}
text = textArray.join(' ');
this.$element.val(text);
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Silence(this, options));
}
});
};
})(jQuery, window, document);
$(function () {
$('.case').silence();
});