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

212
Visualizações
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 Respostas
Responde à pergunta

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