Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
detect spaces in front or behind the word for highlighting

I would like to know how I can give highlight on any letter and if there is a space after or before the word it doesn't eat the space...

function highlight(text) {
  const InputsTexts = document.querySelectorAll('.item-question-faq');


  for (const inputText of InputsTexts) {
    let innerHTML = inputText.innerHTML;
    let index = innerHTML.toUpperCase().indexOf(text.toUpperCase());


    if (index !== -1) {
    innerHTML = innerHTML.substring(0, index) + "<span class='highlight'>" + innerHTML.substring(index, index + text.length) + '</span>' + innerHTML.substring(index + text.length);
    inputText.innerHTML = innerHTML;
    }
  }
}

document.body.onload = function() { highlight('order'); };
.item-question-faq {
  display: flex;
  margin-left: 5px;
  align-items: center;
  font-size: 14px;
  margin-left: 3px;
  margin-right: 3px;
}

.highlight {
  background: yellow;
}
<p  class="item-question-faq webrep-faq" >the order is important</p>

enter image description here

Here is a printout of how it looks when applying the highlightenter image description here

the application can't use Jquery or libraries how can i solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is because you've set the parent element (paragraph) to display: flex. Part of what a flexbox does is decide how to pack in the children elements, and how much space to add between them.

It is almost certainly a mistake to use display: flex for an element that directly contains raw text. Usually, flexboxes are wrapper elements that contain discrete blocks of content.

If you remove this property, allowing the paragraph to use its native display: block mode, the spaces will be preserved as you expected.

function highlight(text) {
  const InputsTexts = document.querySelectorAll('.item-question-faq');


  for (const inputText of InputsTexts) {
    let innerHTML = inputText.innerHTML;
    let index = innerHTML.toUpperCase().indexOf(text.toUpperCase());


    if (index !== -1) {
    innerHTML = innerHTML.substring(0, index) + "<span class='highlight'>" + innerHTML.substring(index, index + text.length) + '</span>' + innerHTML.substring(index + text.length);
    inputText.innerHTML = innerHTML;
    }
  }
}

document.body.onload = function() { highlight('order'); };
.item-question-faq {
  margin-left: 5px;
  align-items: center;
  font-size: 14px;
  margin-left: 3px;
  margin-right: 3px;
}

.highlight {
  background: yellow;
}
<p  class="item-question-faq webrep-faq" >the order is important</p>


minor tip: you can use javascript's forEach to make your code just a little more concise.

Instead of:

const InputsTexts = document.querySelectorAll('.item-question-faq');
for (const inputText of InputsTexts) {
  /* do stuff */
}

Consider:

document.querySelectorAll('.item-question-faq').forEach((inputText) => {
  /* do stuff */
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda