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

148
Visualizações
Interferance when pairing <input> with DOMs

HTML:

<h1 id="ws-title"></h1>
<time datetime="" id="ws-date"></time>

JS:

inp1 = document.querySelector("input[name='ws-date']");
out1 = document.querySelector("#ws-date");
inp1.addEventListener("input", e => {
        out1.textContent=e.target.value;
});

inp2 = document.querySelector("input[name='ws-title']");
out2 = document.querySelector("#ws-title");
inp2.addEventListener("input", e => {
        out2.textContent=e.target.value;
});

Works as expected (i.e: changing input changes corresponsing h2).

However:

function pairListen(name) {
    inp = document.querySelector(`input[name='${name}']`);
    out = document.querySelector(`#${name}`);
    inp.addEventListener("input", e => {
            out.textContent=e.target.value;
    });
}
pairListen("ws-title");
pairListen("ws-date");

Causes <h2 id="ws-date"> to be changed when <input name="ws-title"> is changed and vice versa.

Why is that? All difference is that repeated code is embedded in a function.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because when you declare a variable without a declarator (i.e. var, let, or const) it automatically gets defined in window, this, and globalThis (making those kinds of variables global, accessible anywhere in your code). The second time you do varName = "value";, you're not redeclaring the variable. You're modifying it. You can solve this by applying a declarator.

function pairListen(name) {
    const inp = document.querySelector(`input[name='${name}']`);
    const out = document.querySelector(`#${name}`);
    inp.addEventListener("input", e => {
            out.textContent=e.target.value;
    });
}
pairListen("ws-title");
pairListen("ws-date");
<h1 id="ws-title"></h1>
<input name="ws-title" />
<h2 id="ws-date"></h2>
<input name="ws-date" />

Moral of the story—always declare variables with declarators, or you'll encounter unprecedented results. You can force yourself to follow good style by adding "use strict"; to the top of all your JavaScript files, which will not tolerate writing code that will give you unexpected results.

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