2

I'm trying to set caret to a child span of a div with contenteditable="true",

Below is the code:

placeCaretAt : function(id){
    setTimeout(function(){
        var test = document.getElementById(id);
        var sel = window.getSelection();
        var rang = sel.getRangeAt(0);
        rang.selectNodeContents(test);
        rang.collapse(false);
        sel.addRange(rang);
    },2);
}

This code is working fine if it's executed from browser console.

In lightning, var element = document.getElementById(id); is returning element's proxy due to Locker Service. When passing this element in rang.selectNodeContents(element);, throws an exception:

Uncaught TypeError: Failed to execute 'selectNodeContents' on 'Range': parameter 1 is not of type 'Node'.

Is there any way to get the div element so that it will be accepted in rang.selectNodeContents(element);

I've also tried to put code in static resource to execute but couldn't get over it. Please suggest any solution for it or any way to execute js code outside the lightning life cycle (if possible).

this image describes about the scenario

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.