Permalink
Browse files

Fixed: caret position issue for <input> and <textarea>

  • Loading branch information...
1 parent 078a5ac commit 8bfdd011ce1bb150cb42e8e90e83e37877666e29 @H1D H1D committed Oct 29, 2011
Showing with 36 additions and 2 deletions.
  1. +1 −1 examples/jquery.grewform.min.js
  2. +34 −0 jquery.grewform.js
  3. +1 −1 jquery.grewform.min.js

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -14,7 +14,9 @@
var ignore_codes = [33,34,36,35,45,38,40,37,39];//arrows and others
if (e.keyCode && jQuery.inArray(e.keyCode, ignore_codes) < 0)//ignore this keyUps to let this keys work as expected
{
+ var cp = getCP(this);
jQuery(this).attr('value', this.value);
+ setCP(this,cp);
}
});
@@ -460,6 +462,9 @@
})
}
+
+// ----------------- U T I L S -----------------
+
function arrayfy(obj) {
if (obj.constructor !== Array) {
return [obj];
@@ -475,4 +480,33 @@
return res
}
+
+ function getCP (ctrl) {
+ var cp = 0;
+
+ // IE Support
+ if (document.selection) {
+ ctrl.focus ();
+ var sel = document.selection.createRange ();
+ sel.moveStart ('character', -ctrl.value.length);
+ cp = Sel.text.length;
+ }
+ // Firefox support
+ else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
+ cp = ctrl.selectionStart;
+ }
+ return (CaretPos);
+ }
+ function setCP(ctrl, pos){
+ if(ctrl.setSelectionRange) {
+ ctrl.focus();
+ ctrl.setSelectionRange(pos,pos);
+ } else if (ctrl.createTextRange) {
+ var range = ctrl.createTextRange();
+ range.collapse(true);
+ range.moveEnd('character', pos);
+ range.moveStart('character', pos);
+ range.select();
+ }
+ }
})();
Oops, something went wrong.

0 comments on commit 8bfdd01

Please sign in to comment.