Remove Empty Elements Using Javascript

Well, to remove this empty element, we can use the following script
Examples of empty elements are as follows

Blogger Template - Suppose you have a "div" element or a "span" element or another element where this element does not contain another element or does not contain text, then this element is called an empty element.

Examples of empty elements are as follows:

<div></div>
<span></span>
<p></p>
<a></a>

Well, to remove this empty element, we can use the following script:

function removeAllElement(data) {
    dataCheck = [data];
    for (var i = 0; i < dataCheck.length; i++) {
        var aa = document.querySelectorAll(dataCheck[i]);
        for (var j = 0; j < aa.length; j++) {
            if (aa[j].innerHTML.split("").length == 0) {
                aa[j].remove();
            };
        };
    };
};

//----- the function of the caller -----------
removeAllElement("div", "span", "p", "a");

Remember, the script above is only to delete empty elements where the domain element has been loaded in the browser in the form of a domain document instead of STRING.

Well, what if we want to delete an empty element whose dom is in the form of a string?

To delete an empty element whose source element is in the form of a string is as follows:

Please use the following script:

function removeAllElement(datastring, listElement) {
    dataCheck = listElement.split(",");
    var dataStringDom = document.createElement("div");
    dataStringDom.innerHTML = datastring;
    for (var i = 0; i < dataCheck.length; i++) {
        var aa = dataStringDom.querySelectorAll(dataCheck[i]);
        for (var j = 0; j < aa.length; j++) {
            if (aa[j].innerHTML.split("").length == 0) {
                aa[j].remove();
            };
        };
    };
    return dataStringDom.innerHTML;
};

//----- caller function -----------
// removeAllElement(stringDom,element);

//---examples of use-----------
var stringDom = `<div></div><p></p><a></a><span></span><div>hello</div>`;
var clearDom = removeAllElement(stringDom, "div,span,p,a");

// ---console.log results () --------
console.log(clearDom)

// the result is = <div>hello</div>

If there are any constraints in use, please comment below.