I'm trying to add a filter for the links/divs generated from JSON data.
I already have a search filter working here without a problem IF the data isn't generated via JSON format. But I really need to use JSON for faster loading and processing.
The problem is, it fails to filter the links when doing so. It's supposed to filter divs based on the text inside <a></a> tags, including some hidden texts inside the <span>..
In this example, it can filter only the first item in the JSON, but not for the rest.
// Links Filter
var input = document.getElementById("search-filter-cards");
input.addEventListener("input", searchFilterDivsMenu);
function searchFilterDivsMenu(e) {
var filter = e.target.value.toUpperCase();
var list = document.getElementById("links-list");
var divs = list.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++) {
var a = divs[i].getElementsByTagName("a")[0];
if (a) {
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
divs[i].style.display = "";
} else {
divs[i].style.display = "none";
}
}
}
}
// JSON Data for each links
const maincards = {
"linksList": [{
'name': 'Java Bookmarks',
'title': 'Java Bookmarks',
'favicon': '../assets/links-favicons/java-bookmarks.png',
'url': '#',
'tags': 'Login, Logout, Log-in, Log in, Log-out Log out',
'linkattributes': 'rel="noopener noreferrer" target="_blank"'
},
{
'name': 'Chrono',
'title': 'Chrono',
'favicon': '../assets/links-favicons/chrono.ico',
'url': '#',
'tags': 'Login, Logout, Log-in, Log in, Log-out Log out',
'linkattributes': 'rel="noopener noreferrer" target="_blank"'
},
{
'name': 'Kanban (May)',
'title': 'EDEN Kanban',
'favicon': '../assets/links-favicons/kanban.ico',
'url': '#',
'tags': 'Login, Logout, Log-in, Log in, Log-out Log out, Kanban board',
'linkattributes': 'rel="noopener noreferrer" target="_blank"'
},
]
};
const createMainCardLinks = ({
name,
title,
favicon,
url,
tags,
linkattributes
}) => `
<div class="card link-container">
<img class="link-img" src="${favicon}"/>
<a class="link-title stretched-link" href="${url}" ${linkattributes} title="${title}">${name} <span class="h-0">${tags}</span></a>
</div>
`;
const links = maincards.linksList.map(createMainCardLinks);
links.forEach(links => {
document.getElementById("load-json-data").innerHTML += links;
});
.h-0 {
display: none;
}
<body>
<input id="search-filter-cards" type="text" autocomplete="off" placeholder="Type here to search">
<div id="links-list">
<div class="link-headers"><a class="mb-0">Sample Links</a></div>
<div id="load-json-data"></div>
</div>
</body>
I'm a beginner in JS and I couldn't figure out this problem for some time now. Anyways, thanks in advance for any help.