JSFiddle - React, Tailwind, and code Playground

by David Thomas

HTML

<h3 class="hideIfDivEmpty">title</h3>
<div id="divId"></div>

JavaScript

function hideIfNextEmpty(el) {
    var text = 'textContent' in document ? 'textContent' : 'innerText';
    if (el.nextElementSibling[text].replace(/\s/g, '').length === 0) {
        el.style.display = 'none';
    }
}

hideIfNextEmpty(document.querySelector('h3.hideIfDivEmpty'));