Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda