My setup is below.
It is working, I just have a small issue with it. Currently its checking for child notes that are text (node.nodeType === 3). The problem is, sometimes there are f.e. links or the text is strong in the paragraph and they are getting removed too.
Basically what I want to do is: target (count) everything in a paragraph that is NOT a break. Or in other words something like this: if(node.nodeType !== br)
An Example would look something like this:
<p>
"Text"
<br>
<br>
<strong>
"Text2"
Currently the script would look at the 3 elements between the two texts and would remove the strong element from Text2 but that is not something I want. It should only count the paragraphs and nothing else.
Can somebody help me out please?
const mediaQuery = window.matchMedia('(max-width: 468px)')
// Check if the media query is true
if (mediaQuery.matches) {
const paragraphs = document.querySelectorAll("p");
paragraphs.forEach( elem => {
const nodes = [...elem.childNodes];
nodes.reduce((count, node) => {
if(node.nodeType === 3) {
const isEmpty = node.nodeValue.trim().length === 0;
return isEmpty ? count : 0;
}
count++;
if (count>2) node.remove();
return count;
}, 0);
});
}