Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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? Thanks in advance.

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 />

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading