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

181
Views
Javascript to filter list items and their children by keywords

Can someone correct this js script. When I search "keyword2" or "keyword3" it shows the list items and their children containing these keywords but also shows the siblings and their children which contain different keywords, e.g. "keyword4" or "keyword5". I believe the issue is within the last 5 lines of JS. Also can you make the script non-case sensitive. Thanks.

const $input = document.querySelector("#myInput");
$input.addEventListener("input", (e) => {
  const query = e.target.value;
  const $ul = document.querySelector("ul");
  const $lis = $ul.querySelectorAll("li");
  const $matchedLis = [...$lis].filter(($li) => $li.textContent.match(query));
  [...$lis].forEach(($li) => $li.classList.add("hide"));
  [...$matchedLis].forEach(($li) => {
    $li.classList.remove("hide");
    [...$li.querySelectorAll("li")].map(($li) => $li.classList.remove("hide"));
  });
});
.hide {
  display: none;
}
<html>

<body>

  <input type="text" id="myInput" placeholder="Search through notes" title="Type in a keyword">
  <ul id="List">
    <li>
      Parent 1 keyword1
      <ul>
        <li>
          Child keyword2
          <ul>
            <li>"Grand" Child keyword3</li>
          </ul>
        </li>
        <li>
          Child keyword4
          <ul>
            <li>"Grand" Child keyword5</li>
          </ul>
        </li>
      </ul>
    </li>
    <li>
      Parent 2 keyword6
      <ul>
        <li>
          Child keyword7
          <ul>
            <li>"Grand" Child keyword8</li>
          </ul>
        </li>
        <li>
          Child keyword9
          <ul>
            <li>"Grand" Child keyword10</li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>

</body>

</html>

about 4 years ago · Juan Pablo Isaza
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!