mercredi 20 juin 2018

Javascript won't execute second if statement

For some reason, the function only runs the first if statement. When I flipped the two sections the first one ran and the second one did not. The first set works as intended... 1. Adds image if checkbox is checked and the destination is empty 2. Does nothing if checkbox is checked and the destination has the image already 3. Removes the image if the checkbox is unchecked.

<input type="checkbox" id="mGT">
<input type="checkbox" id="ICE">
<div class="box" id="disp_mGT"></div>
<div class="box" id="disp_ICE"></div>
<button id="show_button">Show System</button>

<script>
var show_button = document.getElementById('show_button');
var show = function() {
//// AC Components
// Get trues and falses
var mGT_ch = document.getElementById('mGT').checked;
var ICE_ch = document.getElementById('ICE').checked;

// ICE Display/Not-Display
var disp_ICE = document.getElementById("disp_ICE");
var disp_ICE_len = disp_ICE.childNodes.length
if (ICE_ch && disp_ICE_len == 0) {
    var ICE_img = document.createElement('img')
    ICE_img.src="http://localhost/Graphics/ICE.png";
    disp_ICE.appendChild(ICE_img);
} else if (ICE_ch && disp_ICE_len == 1) {
} else {    
    disp_ICE.removeChild(disp_ICE.childNodes[0]);
}

// Micro Turbine Display/Not-Display
var disp_mGT = document.getElementById("disp_mGT");
var disp_mGT_len = disp_mGT.childNodes.length
if (mGT_ch && disp_mGT_len == 0) {
    var mGT_img = document.createElement('img')
    mGT_img.src="http://localhost/Graphics/mGT.png";
} else if (mGT_ch && disp_mGT_len == 1) {
} else {    
    disp_mGT.removeChild(disp_mGT.childNodes[0]);
}
}
show_button.addEventListener("click",show);
</script>
</body>




Aucun commentaire:

Enregistrer un commentaire