Skip to content

Commit

Permalink
Fixed: caret position issue for <input> and <textarea>
Browse files Browse the repository at this point in the history
  • Loading branch information
H1D committed Oct 29, 2011
1 parent 078a5ac commit 8bfdd01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/jquery.grewform.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions jquery.grewform.js
Expand Up @@ -14,7 +14,9 @@
var ignore_codes = [33,34,36,35,45,38,40,37,39];//arrows and others 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 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); jQuery(this).attr('value', this.value);
setCP(this,cp);
} }
}); });


Expand Down Expand Up @@ -460,6 +462,9 @@
}) })
} }



// ----------------- U T I L S -----------------

function arrayfy(obj) { function arrayfy(obj) {
if (obj.constructor !== Array) { if (obj.constructor !== Array) {
return [obj]; return [obj];
Expand All @@ -475,4 +480,33 @@


return res 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();
}
}
})(); })();

0 comments on commit 8bfdd01

Please sign in to comment.