I have a script in my code to remove all empty heading tags so they’re not read by screen readers:
$("h1:empty, h2:empty, h3:empty, h4:empty, h5:empty, h6:empty").replaceWith('');
but it’s not picking up heading tags that contain additional empty html like <h2><strong></strong></h2>
How can I also remove these?
5
Answers
You can use
:only-child
pseudo class to select elements which are the only child of a parent element, remove the node, then check if the parent node has.children
, if not remove the parent nodeI tested it on the following code
You need to use
text()
property.Here is how to do this with vanilla JavaScript:
Try this in JavaScript
This script would be helpful: