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

131
Views
Finding and replacing text in HTML not doing anything

I want to find and replace specific words in a text area using user-specified keywords.

This is what my HTML document looks like:

Message: </br>
    <textarea name="text" id="message" rows="3" col="20">Hello 202109</textarea> </br></br>
    Find: <input type="text" name="find" id="find" size="15"> Replace: <input type="text" name="replace" id="replace" size="15"> <br/>
    <button type="button" onclick="findAndReplace()">Find and Replace</button> <br/>

And I'm using this for my Javascript function:

function findAndReplace() {

let find = document.getElementById("find").innerHTML;
let replace = document.getElementById("replace").innerHTML;
let text = document.getElementById("message").innerHTML;
document.getElementById("message").innerHTML = text.replace(find, replace);

}

However, when I open my webpage and try clicking on the "Find and Replace" button nothing happens and no text gets replaced. Inspecting the webpage also shows no errors whatsoever.

What am I doing wrong? Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

That's because you're using .innerHTML instead of .value. .innerHTML gets the inner markup of the element, and can't be used on inputs and textareas. .value is the property you're looking for.

about 4 years ago · Juan Pablo Isaza Report

0

This is the solution to your problem :)

function findAndReplace() {

let find = document.getElementById("find").value;
let replace = document.getElementById("replace").value;
let text = document.getElementById("message").value;
document.getElementById("message").value = text.replace(find, replace);

}
<textarea name="text" id="message" rows="3" col="20">Hello 202109</textarea> </br></br>
Find: <input type="text" name="find" id="find" size="15"> Replace: <input type="text" name="replace" id="replace" size="15"> <br />
<button type="button" onclick="findAndReplace()">Find and Replace</button> <br />

There is also this interesting guide: https://superuser.com/questions/1037389/innerhtml-vs-value-when-and-why

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!