• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

134
Vistas
Match split words from input field with JS

I have an input field value that gets split.

<input type="text" id="findW" value="command prompt" />

const txtValue = document
  .getElementById("findW")
  .value.replace(/[.*+?^${ }()|[\]\\]/g, "\\$&");

const r2 = txtValue.split(" ");
console.log(`R2 with split looks like: ${r2}`);

Every word resulted from splitting must be checked if appears in some elements in order to display them. I did it using jQuery but I would like to use vanilla JS.

let searchInElements = `.some-classes-to-search`;

//How can be rewritten in JS this jQuery below?
$(searchInElements).each(function (i, el) {
  if ($(this).text().match(r2)) {
    showElement(el);
  }
});

Function used to display elements with the words from input:

function showElement(el) {
  el.closest("#idOfEl")
    ? (el.closest("#idOfEl").style.display = "block")
    : null;
}
almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's not all that different really.

let searchInElements = `.some-classes-to-search`;

// Vanilla
document.querySelectorAll(searchInElements).forEach(el => {
  if (el.innerText.trim().match(r2)) showElement(el);
})

function showElement(el) {
  if (parent = el.closest("#idOfEl")) parent.style.display = "block";
}
almost 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda