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

141
Vistas
change styling of certain text characters

I have an input and when user types in it, it loop through an array and check if the value exists and display the value in a p tag. This currently partially works. What I'm trying to do is apply a styling to the characters that match with the input value. The method I tried bellow fails on certain array elements. How can I achieve this to work on any array element?

To understand the question better enter a couple of characters that are included in the array and look at them being displayed.

const myArray = ['test', 'ball', 'cat', 'dog', 'orange', 'the massive theory']

//fails when inputed 'the' should have shown 'the massive theory but shows 'theory'
$('input').on('input', function() {
  const searchString = $(this).val()
  $('p').remove()
  for (var i = 0; i < myArray.length; i++) {
    if (myArray[i].includes(searchString)) {
      $('body').append(`<p>${myArray[i].split(searchString)[0]}<span>${searchString}</span>${myArray[i].split(searchString).pop()}</p>`)
    }
  }
});

$('input').keyup(function(e) {
  if (e.keyCode == 8) {
    if ($(this).val() == "" || $(this).val() == null) {
      $('p').remove()
    }
  }
})
p span {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input />

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

split on the search term, then join back up (adding span to it)

const myArray = ['test', 'ball', 'cat', 'dog', 'orange', 'the massive theory']

//fails when inputed 'the' should have shown 'the massive theory but shows 'theory'
$('input').on('input', function() {
  const searchString = $(this).val()
  $('p').remove()
  for (var i = 0; i < myArray.length; i++) {
    if (myArray[i].includes(searchString)) {

      var str = myArray[i].split(searchString).join("<span>" + searchString + "</span>")
      $('body').append(`<p>${str}</p>`)
    }
  }
});

$('input').keyup(function(e) {
  if (e.keyCode == 8) {
    if ($(this).val() == "" || $(this).val() == null) {
      $('p').remove()
    }
  }
})
p span {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input />

about 4 years ago · Santiago Trujillo Denunciar

0

Your

$('body').append(`<p>${myArray[i].split(searchString)[0]}<span>${searchString}</span>${myArray[i].split(searchString).pop()}</p>`)

splits the phrase bank phrase by the input value - but if the input value is present in more than one place in the phrase, you'll be losing some information in the rendered text. I don't see the split doing anything here other than causing problems. Starting with the phrase and replacing the matching characters with a highlighted span seems better.

for (const phrase of myArray) {
  if (!phrase.includes(searchString)) continue;
  const withSpan = phrase.replace(searchString, '<span>$&</span>');
  $('body').append(`<p>${withSpan}</p>`);
}

const myArray = ['test', 'ball', 'cat', 'dog', 'orange', 'the massive theory']

$('input').on('input', function() {
  const searchString = $(this).val()
  $('p').remove()
  for (const phrase of myArray) {
    if (!phrase.includes(searchString)) continue;
    const withSpan = phrase.replace(searchString, '<span>$&</span>');
    $('body').append(`<p>${withSpan}</p>`);
  }
});

$('input').keyup(function(e) {
  if (e.keyCode == 8) {
    if ($(this).val() == "" || $(this).val() == null) {
      $('p').remove()
    }
  }
})
p span {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input />

about 4 years ago · Santiago Trujillo 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