javascript - Get Cursor Position With Input type number -
i position of cursor in <input>
. here code using:
$("input").keyup(function() { $("<p>") .html("cursor position @ " + $("input").caretposition()) .appendto("#test"); });
and caretposition
defined (just wrapper selectionstart
):
$.fn.caretposition = function() { if (!this.length) return; var input = this[0]; try { return input.selectionstart; } catch(e) { // no need old ie support return 0; } }
html
<input type="number" /> <div id="test"> </div>
unfortunately isn't working in chrome, working if firefox. in chrome, prints 0
time. make me think chrome doesn't support selectionstart
have read support selectionstart
. supported fact if change type="number"
type="text"
starts working. how work consistently across browsers type="number"
? (not ie, don't need support that).
i have read https://stackoverflow.com/a/2897510/3371119 doesn't work me (in chrome).
jsfiddle: http://jsfiddle.net/prankol57/zbaaky5z/2/
edit: have confirmed selectionstart in input
returns true. why error thrown?
as @int32_t pointed out, impossible <input type="number" />
. changed <input type="text" />
, able receive functionality; added own number-validating method so:
function validate(k) { return !isnan(parsefloat(k)) && !isnan(+k); }
in fact, if remove try {} catch {}
statements, chrome throws descriptive error:
uncaught invalidstateerror: failed read 'selectionstart' property 'htmlinputelement': input element's type ('number') not support selection.
Comments
Post a Comment