jquery - How do you set the cursor position using JavaScript in IE8 and older? -


this question has answer here:

i need set cursor position of input. can work fine in chrome, i'm having trouble ie. i've found code works fine in ie9/10, can't find works in ie8 , older.

this have chrome:

var cursorposition = 5; var textarea = $('.the-input-i-need'); textarea[0].selectionstart = cursorposition; textarea[0].selectionend = cursorposition; //now cursor @ 5th spot in input 

anyone know way ie? can use jquery, no other plugins.

there's question regarding getting , setting of caret position in ie on question: get caret position in textarea (ie)

it references answer getting caret position here: how start , end points of selection in text area?

there's healthy amount of code that's specific dealing ie, accomplish you're trying do. note getter function returns selection range, setter can used set selection range. in case, you'd call startoffset equaling endoffset:

setselection(el, startoffset, endoffset);  function setselectionrange(input, selectionstart, selectionend) {   if (input.setselectionrange) {     input.focus();     input.setselectionrange(selectionstart, selectionstart);   }   else if (input.createtextrange) {     var range = input.createtextrange();     range.collapse(true);     range.moveend('character', selectionend);     range.movestart('character', selectionstart);     range.select();   } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -