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

149
Visualizações
Detect as soon as the input field is populated through barcode scanner

I have two input fields productName and quantity, productName field is getting populated using a barcode scanner(don't have access to code), my requirement is to detect as soon as the value gets populated in productName fields so that I can parse the value and populate the second field quantity.

<input type="text" id="productName" name="productName">
<input type="text" id="quantity" name="quantity">

Since I don't have access to code that scans the barcode and populate the productName field, there is one possibility which I think of is repeatedly checking if the field has value or not like below

myVar = setInterval(checkValue, 1000);

function checkValue(){
var productName= document.getElementById("productName");
console.log("running");
if (productName && productName.value) {
  document.getElementById("quantity").value=productName.value;
  clearInterval(myVar);
}
}

But I am looking for a better approach in vanilla Javascript, is there an alternate solution?

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

0

You can use onchange.

Example:

const productNameField = document.querySelector('#productName');
const quantityField = document.querySelector('#quantity');

const populateQuantityField = () => {
  quantityField.value = productNameField.value;
}

productNameField.addEventListener('change', populateQuantityField);

Further Reading:

  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event
  • https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onchange
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