I have built a code that filters the data until the first occurrence. After that, it is returning all sequential data which not required. Can you please help me with the approach to solve this!!
filter URL is localdirector/index.html?filter=code2. The value to be returned only code2. but for me, code3 is also returning. Can anyone please help me with it!!
<html>
<body>
<ul class="portfolio-filters list-inline">
<li class="filter active code1" data-filter="all">code1</li>
<li class="filter code2" data-filter="webdesign">code2</li>
<li class="filter code3" data-filter="responsive">code3</li>
<li class="filter code4" data-filter="wordpress">code4</li>
</ul>
<script>
const url = new URL(window.location.href)
const currentFilter = url.searchParams.get('filter')
if (currentFilter != null) {
const collection = document.getElementsByClassName('filter')
for (let item of collection) {
if (item.classList.contains(currentFilter)) {
item.style.display = 'block'
break
}
item.style.display = 'none'
}
}</script>
</body>
</html>
You exit the loop so the ones after it do not run
You can just do it with a selector instead of a loop