javascript - Focus Input Mid-Way Through Text -


i know it's pretty easy focus input using javascript/ jquery, possible focus input on character?

so take input:

inputexample

would possible focus element , have cursor placed after first sentence, example?

sure :

$('#inputid').focus().get(0).setselectionrange(12, 12); 

works in browsers, older ie uses createtextrange , move().

fiddle

edit:

five minute plugin:

$.fn.setcaret = function(pos) {     return this.each(function() {         var elem = this,             range;          if (elem.createtextrange) {             range = elem.createtextrange();             range.move('character', pos);         } else {             if (elem.selectionstart !== undefined) {                 elem.setselectionrange(pos, pos);             }         }     }); } 

to called like:

$('#inputid').focus().setcaret(12); 

fiddle


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -