Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!