I’m using the CMS to pull dynamic content into a list of personal links on a team page. (screenshot)
There are 8 list items.
If the link field is empty, I have a filter setup to hide the empty list item. (screenshot)
My question is…
Is there a way to remove the <li> tag altogether? This way, a screen reader will read “1 of 3” instead of “1 of 8”?
Here is my public share link: LINK
([how to access public share link][2])
Awesome, just be aware that that code will remove any <li> that contains a conditionally-hidden element as its immediate child. I used the > to restrict it a bit. If that ever becomes an issue, you could add a “hide if empty” class to your CMS bound elements, adjust the jQuery selector so that it will only remove LI’s containing those specifically-tagged elements.
// get the element
const list = document.querySelector("#list"); //?
// remove empty list items
list.querySelectorAll("li").forEach((li) => {
if (li.textContent === "") {
li.remove();
}
});
console.log(list); //?
OR
/ get the elements
const list = Array.from( document.querySelectorAll("#list li"));
// remove empty list items
list.forEach((item) => {
if (item.textContent === "") {
item.remove();
}
});
Heh heh, I knew you would be onto that. I need to get more comfortable with plain JS for client-side scripting, it’s often a better choice overall, but it often feels so unnecessarily verbose for this kind of work.
Yeh I like to do these simple brain exercises. JS is more explanatory and more code but it is for me clear what I see (sometimes) JQ is for me another universe and I do not like its syntax anyway. I’m fighting with TS in Svelte and that is true hell for me.