Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda