JSFiddle - React, Tailwind, and code Playground

by jfriend00

HTML

<p>
    <br>
    <br>
    JQuery problems again...
    <br>
    <br>
    Why me...?
</p>

JavaScript

$("p").each(function() {
    var children = this.childNodes;
    var removals = [], child, i;
    for (i = 0; i < children.length; i++) {
        child = children[i];
        // nodeType 1 is ELEMENT_NODE
        if (child.nodeType == 1) {
            if (child.nodeName.toLowerCase() == "br") {
                removals.push(child);
            }
        }
        // nodeType 3 is TEXT_NODE
        else if (child.nodeType == 3) {
            // stop at first non whitespace text node
            if (child.nodeValue.match(/\S/)) {
                break;
            }
        }
    }
    // now remove the nodes we collected for removal
    // remove outside the first loop because childNodes is a live array
    // and we don't want it changing while iterating it
    for (i = 0; i < removals.length; i++) {
        removals[i].parentNode.removeChild(removals[i]);
    }
});