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

143
Visualizações
Bold the selected text

I have an HTML5 file and I am trying to make an app like Google Docs using create-electron-app.

I have a textarea tag for the input, and I am trying to bold the text the user has selected. I have the getSelectedText() function working and It is returning a string like expected, but I need to bold that text inside of the textarea. If you have used Google Docs you should know what I mean.

Here is the code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Make word documents!</title>
    <link rel="stylesheet" href="index.css" />
  </head>
  <body>
    <textarea class="userInput"></textarea>

    <input type="button"
    value="Bold" 
    onmousedown="bold()">

    <script>

function getSelectedText() {
    if (window.getSelection) {
        return "" + window.getSelection();
    } else if (document.selection && document.selection.type == "Text") {
        return document.selection.createRange().text;
    }
    return "";
}


      function bold() {
        var userInput = getSelectedText();
        // userInput.style.fontWeight = "bold";

        // print the type of the userinput variable
        console.log(typeof userInput);
        
      }
    </script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

QuestionSign in to vote

0

Sign in to vote

User1937363675 posted

Hi Francis

Actually I think you should use "span" instead of "textarea",because according to some material,Web Editor all almost use span.I wrote a short example for you,hope that can help you!

--------------

<html>
<head>
<script language="javascript">
function test(e)
{
   mytext.innerHTML="<b>"+mytext.innerText+"</b>";
}


</script>
</head>
<body>
<span contenteditable id="mytext" style="height:200;width:95%;border:1px solid black">
HTML Editor
</span>
<input type="button" value="Bold" onclick="test()" />
</body>


</html>

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