Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
How to move cursor into text node and make it editable if there is no preexisting content using Range and Selection Objects?

I am trying to make text changes based on when the user clicks the "bold" button using the Range and Selection Objects. When the text node is empty, the range that I have set using setStart() and setEnd() seems to not be working because the cursor does not move into the text node, but rather stays at its initial text node that is already given within my contenteditable div. However, If I add some preexisting content to the text Node, the range is correct,the cursor moves into the newly-created text node, and the content is bold as well as my additional input. So how do I get the cursor to go into the text Node without any content? Thanks.

Here is a code snippet:

function setBold() {    
    let parent = document.querySelector('.current'); /*contenteditable parent element*/ 
    let sel = window.getSelection();
    let selRange = s.getRangeAt(0); 
    let bold = document.createElement('b'); 
    let range = document.createRange();
    /*if the node has content this solution works, but in this example it does not currently*/
    let node = document.createTextNode("");
    parent.appendChild(node);
    range.setStart(node,0);
    range.setEnd(node,node.length);
    range.surroundContents(bold);
    sel.removeAllRanges();
    sel.addRange(range);
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda