So there are a few different ways you can achieve this. Within your javascript where you add the content to the divs you can add else statements to hide/delete the redundant divs, or after you've added all the required content you could loop through your divs and hide/delete any that don't have content.
I would recommend against deleting the divs, as that would mean you have to add them each time you reset your from. Instead, you can add the style display: none
This will remove them from the DOM, and thus remove the whitespace.
Another piece of advice - Don't use <br />
tags after your divs. Divs by default are blocks, and will force a new line after. If you need more space, it would be better to add bottom margin.
Below is an edited version of your codepen that hides any of the divs that are not required.
function checkbox() {
var finance = document.getElementById("finance").checked;
var HR = document.getElementById("HR").checked;
var Procurement = document.getElementById("Procurement").checked;
var Appraisal = document.getElementById("Appraisal").checked;
var HS = document.getElementById("HS").checked;
var MSS = document.getElementById("MSS").checked;
if (finance){
document.getElementById("res1").innerHTML =
"<strong>Finance</strong><br/>Finance Results";
}else{
document.getElementById("res1").classList.add("hide");
}
if (HR){
document.getElementById("res2").innerHTML = "<b>HR</b><br/>HR Results";
}else{
document.getElementById("res2").classList.add("hide");
}
if (Procurement){
document.getElementById("res3").innerHTML =
"<b>Procurement</b><br/>Procurement Results";
}else{
document.getElementById("res3").classList.add("hide");
}
if (Appraisal){
document.getElementById("res4").innerHTML =
"<b>Appraisal</b><br/>Appraisal Results";
}else{
document.getElementById("res4").classList.add("hide");
}
if (HS) {
document.getElementById("res5").innerHTML =
"<b>Health & Safety</b><br/>Health & Safety Results";
} else {
document.getElementById("res5").classList.add("hide");
}
if (MSS) {
document.getElementById("res6").innerHTML = "<b>MSS</b><br/>MSS Results";
} else {
document.getElementById("res6").classList.add("hide");
}
return false;
}
input[type="checkbox"] + label {
position: absolute;
width: 30px;
height: 30px;
border: 5px solid #555;
border-radius: 50%;
left: 50px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
-webkit-transition: all ease-out 200ms;
transition: all ease-out 200ms;
text-indent: 45px;
font: normal normal 25px/40px "Helvetica";
white-space: nowrap;
color: #555;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"] + label:after {
content: "";
position: absolute;
width: 0px;
height: 13px;
border-bottom: 5px solid #22b573;
border-left: 5px solid #22b573;
top: 25%;
left: 50%;
-webkit-transform-origin: bottom left;
transform-origin: bottom left;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
opacity: 0;
-webkit-transition: all ease-out 200ms;
transition: all ease-out 200ms;
}
input[type="checkbox"]:checked + label {
border: 5px solid #22b573;
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
width: 35px;
}
#finance {
display: none;
}
#HR {
display: none;
}
#Procurement {
display: none;
}
#Appraisal {
display: none;
}
#HS {
display: none;
}
#MSS {
display: none;
}
.hide{
display: none;
}
<p>I need information about:</p><br/>
<form>
<input type="checkbox" id="finance" name="Manager" value="Finance"><label for="finance">Finance</label><br/><br/><br/>
<input type="checkbox" id="HR" name="Manager" value="HR"><label for="HR">HR</label><br/><br/><br/>
<input type="checkbox" id="Procurement" name="Manager" value="Procurement"><label for="Procurement">Procurement</label><br/><br/><br/>
<input type="checkbox" id="Appraisal" name="Manager" value="Appraisal"><label for="Appraisal">Appraisal</label><br/><br/><br/>
<input type="checkbox" id="HS" name="Manager" value="Hs"/><label for="HS">Health & Safety</label><br/><br/><br/>
<input type="checkbox" id="MSS" name="Manager" value="MSS"><label for="MSS">MSS</label><br/><br/><br/>
<input type="submit" value="See Training" size="30" onClick="return checkbox();">
<input type="submit" value="Reset" size="30" onClick="return reset();">
</form><br/>
<div id="mandatory"><b>Mandatory</b><br/>The mandatory training all managers have to do</div><br/>
<div id="res1"></div>
<div id="res2"></div>
<div id="res3"></div>
<div id="res4"></div>
<div id="res5"></div>
<div id="res6"></div>
` between them. (in the codepen). – emed Nov 09 '17 at 14:42