Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!