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

147
Visualizações
How to clear the value of an input if there is no results or if it has a null value?

I am trying to clear the value of an input depending if it finds or not an id, if it finds an existing id, js updates the value of an input, but if it doesn't it keeps the last one found but I need to have the value clear, can someone tell me what is wrong:

function driverdata(valueid)
{
    var numero_id = valueid;
    //console.log(valueid)
    var idselect = document.getElementById('driver'+id_number).value;
    document.getElementById("idinsearch"+ id_number).value = idselect;
    //console.log(idselect);
    var placa = document.getElementById("searchable"+idselect).value;
    console.log(placa);
    if (placa != null) {
        document.getElementById("placa"+ id_number).value = placa;
    } else {
        document.getElementById("placa"+ id_number).value = "";
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In the method driverdata you don't define variable id_number so it's undefined when you try get element by id

So if id_number is equal to the parameter of the method you can directly use it

moreover to clear value you are right it's elem.value = ""

withour yout html i can propose you the following one => your script run

function driverdata(numberId)
{
    var idselect = document.getElementById('driver'+numberId).value;
    document.getElementById("idinsearch"+ numberId).value = idselect;
    var placa = document.getElementById("searchable"+idselect).value;
    if (placa != null) {
        document.getElementById("placa"+ numberId).value = placa;
    } else {
        document.getElementById("placa"+ numberId).value = "";
    }
}
<div onclick="driverdata(1)">
click me<br/>
 driver<input id="driver1" value="1"/><br/>
  idinsearch<input id="idinsearch1"/><br/>
  <div id="searchable1">
    input that will be clear <input id="placa1" value="test"/>
  </div>
</div>

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