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

266
Visualizações
How to get user input to look in string

I want to write a paragraph and then have a search bar to look up if there is a given word in the parapragh. If I write "are" in the search bar, it should tell me through alert "The word is in the text." And if I write something that is not in the text, it should tell me "No, it's not there.".

Could look something like:

<p id="p1"> Hi, how are you?</p>
<input type="text" placeholder="Type something..." id="myInput">
  <button type="button" onclick="getInputValue();">Search</button>

<script>
function getInputValue() {
  let text = document.getElementById("p1");
  let textElement = document.getElementById(myInput).value;

  if (text.indexOf("textElement") >-1){
    return true;
  } else if {
    (text.indexOf("textElement") =-1)
    return false;
  }

  console.log(myInput);
}
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I have read your question very carefully and I think following is your answer:

function getInputValue() {
      let text = document.getElementById("p1").textContent;
      let textElement = document.getElementById('myInput').value;

        if (text.toLowerCase().indexOf(textElement.toLowerCase()) !== -1){
            alert("The word is in the text.");
        } else {
            alert("No, it's not there.");  
        }
      }
    <p id="p1"> Hi, how are you?</p>
    <input type="text" placeholder="Type something..." id="myInput">
      <button type="button" onclick="getInputValue();">Search</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

The approach was already quite good. Enclosed is your revised Javascript. It is case insensitive.

function getInputValue() {
  
  let text = document.getElementById("p1").innerHTML;
  
  let textElement = document.getElementById('myInput').value;
  
  if (text.toLowerCase().indexOf(textElement.toLowerCase()) !== -1){
    alert("word found.");
  } else if (text.indexOf(textElement.toLowerCase()) === -1) {
    alert("nothing founded");  
  }
  
  console.log(myInput);
}
<p id="p1"> Hi, how are you?</p>
<input type="text" placeholder="Type something..." id="myInput">
<button type="button" onclick="getInputValue()">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