Skip to content

Commit 8d1b328

Browse files
committed
Update jquery.numeric.js, saving selection end, allowing keyboard ctrl+A and keyboard selection
1 parent 53efaf1 commit 8d1b328

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

numeric/jquery.numeric.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ $.fn.numeric.keyup = function(e)
144144
{
145145
// get carat (cursor) position
146146
var carat = $.fn.getSelectionStart(this);
147+
var selectionEnd = $.fn.getSelectionEnd(this);
147148
// get decimal character and determine if negatives are allowed
148149
var decimal = $.data(this, "numeric.decimal");
149150
var negative = $.data(this, "numeric.negative");
@@ -217,7 +218,7 @@ $.fn.numeric.keyup = function(e)
217218
}
218219
// set the value and prevent the cursor moving to the end
219220
this.value = val;
220-
$.fn.setSelection(this, carat);
221+
$.fn.setSelection(this, [carat, selectionEnd]);
221222
}
222223
};
223224

@@ -253,6 +254,16 @@ $.fn.getSelectionStart = function(o)
253254
} else { return o.selectionStart; }
254255
};
255256

257+
// Based on code from http://javascript.nwbox.com/cursor_position/ (Diego Perini <dperini@nwbox.com>)
258+
$.fn.getSelectionEnd = function(o)
259+
{
260+
if (o.createTextRange) {
261+
var r = document.selection.createRange().duplicate()
262+
r.moveStart('character', -o.value.length)
263+
return r.text.length
264+
} else return o.selectionEnd
265+
}
266+
256267
// set the selection, o is the object (input), p is the position ([start, end] or just start)
257268
$.fn.setSelection = function(o, p)
258269
{

0 commit comments

Comments
 (0)