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

211
Views
How do I get letters in the inputs and change writing in the div?

I tried code snippet below entering i in the first and x in the second input but Change this writing didn't change into Change thxs wrxtxng

Any help would be much appreciated!

var d = document.getElementById("d").innerHTML
var Btn = document.getElementById("btn");
var inp1 = document.getElementById("i1").value;
var inp2 = document.getElementById("i2").value;
Btn.addEventListener("click",change);
function change() {
    var yeni = d.replaceAll(inp1,inp2);
    document.getElementById("d").innerHTML = yeni;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div id="d">Change this writing</div><br>
<input id="i1" type="text">&nbsp&nbsp&nbsp<input id="i2" type="text"><br><br>
<button id="btn" class="btn btn-danger">Change</button>

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

0

If you place console.log(inp1, inp2); inside the change function, you'll see that there don't hold the expected value.

You're defining them before the user has any time to insert the value.

Easiest fix would be to call getElementById inside the function when the button is pressed:

var Btn = document.getElementById("btn");
Btn.addEventListener("click",change);

function change() {
    var inp1 = document.getElementById("i1").value;
    var inp2 = document.getElementById("i2").value;
    var d = document.getElementById("d");
   
    var yeni = d.innerHTML.replaceAll(inp1, inp2);
    d.innerHTML = yeni;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div id="d">Change this writing</div><br>
<input id="i1" type="text" >&nbsp&nbsp&nbsp<input id="i2" type="text"><br><br>
<button id="btn" class="btn btn-danger">Change</button>

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!