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

132
Visualizações
How to update from input type date to input type text field dynamically in JavaScript

I want to create a input field with date but when user type his name for search then it also work in the same field.

More specifically I want make a dynamic input field with text and date input field in a search filter button in JavaScript.

Is it possible to create a field with date and later when user want to search by name then he able to search the name in same field and if he wants to search filter by date he also able to search in same field.

If it's possible then how can I do this?

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

0

You can use setAttribute(attr, value) property to change the attribute types. More can be found here

Example using buttons:

function searchByName(){
  document.getElementById("search").setAttribute("type","text");
}

function searchByDate(){
  document.getElementById("search").setAttribute("type","date");
}
<button onclick="searchByName();">Search by name</button> 
<button onclick="searchByDate();">Search by date</button>

<input type="date" name="search" id="search" />

EDIT

If you want to dynamically change based on input, you can try this:

const searchInput = document.getElementById("search");

searchInput.addEventListener("keydown",(e)=>{
    const isNumber = /\d/.test(e.key)
    if(isNumber){
        searchByDate();
    }
    else{
        searchByName()
    }
},{once:true});

function searchByName(){
  document.getElementById("search").setAttribute("type","text");
}

function searchByDate(){
  document.getElementById("search").setAttribute("type","date");
}
<input type="date" name="search" id="search" />

PS: The above script will run only once. If your user has pressed the first key a number, then the input will remain as date otherwise, it will change to text

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