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

183
Visualizações
How to keep a getSelection active using javascript

I am trying to change font-size of the selected text in my div by clicking a button but when I press my button i lose the highlight/selection. How can I keep the text highlighted/selected while I also press my button.

    function sizeup(event) {
        event.preventDefault();
        var selectedText = document.getSelection();
        //please compelete after here😢😢😢😢😢
        
    }
<!--Html here-->
<div contenteditable="true" style="height:200px;">
<button onclick="sizeup(event)">SIZEUP</button>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can select the element by its ID and then manipulate the font-size :

function changeFontSize() {
    var selectedText = "";
    if (window.getSelection) {
      selectedText = window.getSelection().toString();
      var newContent = document.getElementById('myDiv').innerHTML.replace(selectedText, '<span style="font-size:30px;">'+selectedText+'</span>');
      document.getElementById('myDiv').innerHTML = newContent;
    }
}
<div id="myDiv" contenteditable="true" style="height:100px;">asdasd</div>
<button onclick="changeFontSize()">SIZEUP</button>

about 4 years ago · Juan Pablo Isaza Relatório
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