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

154
Visualizações
JavaScript delete sentences add new default sentences with regex

Hi I'm learning java script. I am currently trying to figure out the regex. I want to try the option when I enter something in the text area field, to automatically click on the button to delete the text entered in the field and print the regex text some that will be the default, and to replace HTML entities with their visual display: & amp; in &, & nbsp; with a space, & quot; with quotation marks.

<!DOCTYPE html>
<html>
<body>

<textarea id="demo" name="w3review" rows="4" cols="50">

  </textarea>
  <br>
  <button id="btnText" onclick="tipka()">click</button>
<script>
function tipka(){
let str = document.getElementById("demo").innerHTML; 
let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
document.getElementById("demo").innerHTML = res;}
</script>

</body>
</html>

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

0

If i understand question there should be document.getElementById("demo").value not document.getElementById("demo").innerHTML

<!DOCTYPE html>
<html>
<body>

<textarea id="demo" name="w3review" rows="4" cols="50">

  </textarea>
  <br>
  <button id="btnText" onclick="tipka()">click</button>
<script>
function tipka(){
let str = document.getElementById("demo").value; 

let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
document.getElementById("demo").value= res;
}
</script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

The solution requires a few changes :

  1. Inside tipka, the function should read the value of the textarea instead of the innerHTML.
  2. Similarly, while replacing the text, set the value of the textfield instead of innerHTML.

See Setting innerHTML vs. setting value with Javascript

Here's the snippet working with the suggested changes :

<!DOCTYPE html>
<html>
<body>

  <textarea id="demo" name="w3review" rows="4" cols="50">
  </textarea>
  <br>
  <button id="btnText" onclick="tipka()">click</button>

  <script>
    function tipka() {
      let str = document.getElementById("demo").value;
      let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
      document.getElementById("demo").value = res;
    }
  </script>

</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