Here's one way to do it:
$("#starting-point").find("br").each(function() {
if (this.previousSibling && this.previousSibling.nodeName.toUpperCase() == 'BR') {
$(this).remove();
}
});
That finds all of the br
elements within the given container (in my case, the element with the id
"starting-point"
). Then it loops through them in document order. If we have one that has an immediate previous sibling that's a br
element, we remove it. We leave it alone if the thing just in front of it is not a br
element (such as a text node, or a non-br
element). Note that this doesn't consider <br> <br>
to be two consequtive elements, because there's a space between them.