forked from hugosbg/jquery-simple-txt-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-simple-txt-counter.min.js
9 lines (9 loc) · 1.07 KB
/
jquery-simple-txt-counter.min.js
1
2
3
4
5
6
7
8
9
/**
* jQuery Simple Text Counter
*
* @homepage https://github.com/hugosbg/jquery-simple-txt-counter#readme
* @author Hugo Gomes <hugo.msn@msn.com>
* @version 0.1.6
* @license MIT
*/
!function(t){t.fn.simpleTxtCounter=function(e){const n=t.extend({after:void 0,maxLength:void 0,countText:void 0,countElem:"<div/>",lineBreak:!0},e),i=(e,i,l,s)=>{const{after:o,countText:u,countElem:r}=n;let a=u?`${u} ${i}`:i;l&&(a+=` / ${l}`);const c=t(r).attr("id",s).text(a),h=e.closest(o);if(h.length){let t=h.next("[id^=simple-txt-counter]");t.length?t.text(a):h.after(c)}else{let t=e.next("[id^=simple-txt-counter]");t.length?t.text(a):e.after(c)}};return this.each(function(e){const l=t(this),s=parseInt(l.attr("maxlength")||n.maxLength),o=`simple-txt-counter-${e}`;i(l,this.value.length,s,o),l.on("input",function(){i(l,this.value.length,s,o),this.value&&s&&(!1===n.lineBreak?this.value=this.value.replace(/(\r\n|\n|\r)/gm," ").slice(0,s):this.value=this.value.slice(0,s))}).on("keypress",function(t){const e=t.which||t.keyCode;if(!1===n.lineBreak&&13===e)return t.preventDefault(),!1})})}}(jQuery);