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

178
Visualizações
Highlight the word in array

I have 2 issues:

  1. Array split functionality as I cannot use it not sure why?
  2. Words are not highlighting.

const text = [
        `Jelly sweet roll jelly beans biscuit pie macaroon chocolate donut. Carrot cake caramels pie sweet apple pie tiramisu carrot cake. Marzipan marshmallow croissant`  
    ]; // this is what i want to use instead of 'p' but also below not working
    function selectWord() {
        //const p = document.querySelector('p');
        text.innerHTML = text.innerText
            .split('')
            .map((word) =>
                word.length > 5 ? `<span class="lightext">${word}</span>` : word
            )
            .join('');
    }
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .lightext {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <p>This is the extremely long paragraph that we want to highlight all words longer than eight characters in.</p>
</body>
<script src="app1.js"></script>

</html>

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

0

So, there are a few issues. I assume you changed the code for testing because you're not calling the function selectWord() anywhere (and the element is commented out).

You cannot use .split('') because that breaks strings into individual characters, not words, so everything has a length of 1. You need to change both your split and join to be .split(' ') and .join(' ').

Please also note, your text variable is an array, not a DOM object. Therefore it does not posses the innerHTML and innerText properties

The correct script would be.

function selectWord(str) {
  const el = document.querySelector("p")
  const highlightedText = str.split(' ').map((word) => word.length > 5 ? `<span class="lightext">${word}</span>` : word).join(' ')
  el.innerHTML = highlightedText
}

selectWord('this is a longer string than normal')
about 4 years ago · Juan Pablo Isaza Relatório

0

I simply fixed the obvious bugs in your code. Most notably, splitting words on spaces (where you had an empty string) and then reassembling them by the same.

    function selectWord() {
        const p = document.querySelector('p');
        console.log(p.innerText.split(' '));
        p.innerHTML = p.innerText
            .split(' ')
            .map((word) =>
                word.length > 5 ? `<span class="lightext">${word}</span>` : word
            )
            .join(' ');
    }
    selectWord();
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .lightext {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <p>This is the extremely long paragraph that we want to highlight all words longer than eight characters in.</p>
</body>
<script src="app1.js"></script>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. selectWord function was never called
  2. The text should not be an array but a string
  3. In split and in join, you should have a string with a space to break up the words. An empty string will split all characters

const text = `Jelly sweet roll jelly beans biscuit pie macaroon chocolate donut. Carrot cake caramels pie sweet apple pie tiramisu carrot cake. Marzipan marshmallow croissant`; // this is what i want to use instead of 'p' but also below not working
    function selectWord() {
        const p = document.querySelector('p');
        p.innerHTML = text
            .split(' ')
            .map((word) =>
                word.length > 5 ? `<span class="lightext">${word}</span>` : word
            )
            .join(' ');
    }
    selectWord();
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .lightext {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <p>This is the extremely long paragraph that we want to highlight all words longer than eight characters in.</p>
</body>
<script src="app1.js"></script>

</html>

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