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

171
Visualizações
Getting the value from an HTML input and use for javascript function

I'm not a professional coder, and I have an issue that might have a very simple fix.
So here is my code so far. It is very basic. I'm trying to get the code from the html input, and use that to carry out the javascript. For example, if a person types "Hydrogen" into the input, I want it to take them to a hydrogen page, but if they type in "helium", I want to take them to the helium page. Here's the code that I have written, but everytime I try it, no matter what I type, it takes me to the helium page. Can anyone please help me fix this issue. Thank you

<body>
  <input id="search" placeholder="Search for an element">
  <button>Search</button> 
</body>
<script type="text/javascript">
  document.querySelector("button").addEventListener("click", function() {        
    if (document.getElementById('search').value === 'Hydrogen'||'H'||'1') {
      window.location.href='./elements/hydrogen.html'
    }
    if (document.getElementById('search').value === 'Helium'||'He'||'2'){
      window.location.href='./elements/helium.html'
    }
 })
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Because any non-empty string is truthy in JavaScript. So conditions like this will always be true:

document.getElementById('search').value === 'Hydrogen'||'H'||'1'

Because 'H', being a non-empty string, is interpreted as "true".

Programming languages are not natural human languages relying on human intuition. Everything in a programming language must be explicitly defined using unambiguous logic. So your conditions would instead be:

document.getElementById('search').value === 'Hydrogen' ||
document.getElementById('search').value === 'H' ||
document.getElementById('search').value === '1'

That is, each condition is tested fully and independently, and the results are tested against the logical || operators.

You can of course shorten this in a variety of ways. For example:

['Hydrogen', 'H', '1'].includes(document.getElementById('search').value)
about 4 years ago · Juan Pablo Isaza Relatório

0

this way

const
  button = document.querySelector('button') 
, search = document.getElementById('search')
  ;
button.onclick = e =>
  {
  if (['Hydrogen','H','1'].includes( search.value ))
      window.location.href='./elements/hydrogen.html'
      
  if (['Helium','He','2'].includes( search.value ))
      window.location.href='./elements/helium.html'
  } 
  <input id="search" placeholder="Search for an element">
  <button>Search</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