How can I dynamically hide the heading above a div if that div is empty? The content of the div will change based on user input. I would like the heading1 to appear immediately after a button click event.
I am inclined towards a CSS solution, although the page already utilizes jQuery and JavaScript extensively, so I am open to all suggestions.
If assigning unique IDs to all headings simplifies the task, I am willing to go down that route as well.
function addcontent()
{
document.getElementById("Content1").innerHTML = "Extra Content";
}
$(".rulescontent:empty").parent().hide();
$(!".rulescontent:empty").parent().show();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<h1>heading1</h1>
<div style="font-size:90%" id="Content1" class="rulescontent"></div>
</div>
<div>
<h1>heading2</h1>
<div style="font-size:90%" id="Content2" class="rulescontent"></div>
</div>
<button type='button' onclick="addcontent()">add content</button>
Other Stackoverflow solutions do not handle scenarios where the content reappears once it is no longer empty.