jquery - Set cursor position at the beginning of the line -
i know question has been answered in site, have conflict current code i've used. (find below)
i have default value in text filed "example.company.com" , set focus clear example value.
jquery(document).ready(function() { jquery.fn.cleardefault = function() { homecoming this.focus(function() { if( this.value == this.defaultvalue ) { this.value = ".company.com"; } }).blur(function() { if( !this.value.length ) { this.value = this.defaultvalue; } }); }; jquery(".clearit input, .clearit textarea").cleardefault(); }); i need set cursor origin of line when user clicks , example disappeared, wasn't able that. i'm not expert in jquery, asking here help.
thanks!
there 2 ways set cursor particular position in input:
setselectionrange(startposition, endposition);: selects text between 2 provided positions. if start , end same, cursor placed before character @ position. so, place cursor @ origin setselectionrange(0, 0);. setcursorposition(position);: moves cursor position provided. so, place cursor @ origin setcursorposition(0);. after having tried code, seems on focus (especially via click), cursor placed @ point of click , setselectionrange and/or setcursorposition gets overridden.
one possible workaround wrap in timer induce faux-delay. this:
demo: http://jsfiddle.net/abhitalks/vu5yf/5/
relevant code:
jquery.fn.cleardefault = function() { homecoming this.focus(function(e) { if( this.value == this.defaultvalue ) { this.value = ".company.com"; var t = this; window.settimeout(function() { t.setselectionrange(0, 0); // t.setcursorposition(0); }, 0); } }).blur(function(e) { if( !this.value.length ) { this.value = this.defaultvalue; } }); }; jquery(".clear").cleardefault(); you may seek increment timeout little amount tweak it, if required. but, don't create high otherwise there's possibility user able input character before timer fires.
jquery
No comments:
Post a Comment