javascript - Cursor position abnormally situated at the end of the input value in Chrome -
i coded jquery function remove automatically single , double quotes while writing text in input box. in firefox goes in chrome, if want add beginning of text, not allowed. because cursor situated @ end of input value. don't know how fix it. here code:
$.fn.removequotes = function() { var elem = $(this); elem.bind("focus propertychange change click keyup input paste", function(event) { settimeout(function () { elem.val(elem.val().replace(/['"]/g, "")); }, 1); }); };
edit: after comments, tried this:
$.fn.removequotes = function() { var elem = $(this); elem.bind("focus propertychange change click keyup input paste", function(event) { // store current positions in variables var start = this.selectionstart, end = this.selectionend; settimeout(function () { elem.val(elem.val().replace(/['"]/g, "")); }, 1); // restore variables... this.setselectionrange(start, end); }); };
but nothing changed.
here's crude version build on - https://jsfiddle.net/sanjeevi/79gun3g0/1/
<div> <input id="text-box" type="text"> </div> $.fn.removequotes = function() { var elem = $(this); elem.bind("keyup", function(event) { var start = elem.caret(); console.log(start); elem.val(elem.val().replace(/['"]/g, "")); setcaretposition('text-box',start); }); }; function setcaretposition(elemid, caretpos) { var elem = document.getelementbyid(elemid); if(elem != null) { if(elem.createtextrange) { var range = elem.createtextrange(); range.move('character', caretpos); range.select(); } else { if(elem.selectionstart) { elem.focus(); elem.setselectionrange(caretpos, caretpos); } else elem.focus(); } } } $(document).ready(function(){ $("#text-box").removequotes(); });
Comments
Post a Comment