Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

JavaScript: Set the cursor to a particular position in textbox?

I am writing a plugin for CKEDITOR which creates a custom tag. Once this custom tag has been set, I need put my cursor in between it.

Is it possible to put the cursor to a specific location inside a textbox using JavaScript or jQuery?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Please note it's not a textbox and in the background it's a textarea to "save" the HTML generated

https://ckeditor.com/old/forums/CKEditor-3.x/SOLVED-How-set-cursor-position-CKEditor

editor.focus();
var selection = editor.getSelection();
var range = selection.getRanges()[0];
var pCon = range.startContainer.getAscendant({p:2},true); //getAscendant('p',true);
var newRange = new CKEDITOR.dom.range(range.document);
newRange.moveToPosition(pCon, CKEDITOR.POSITION_BEFORE_START);
newRange.select();

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.