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

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

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 Report

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 Report

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