Update: I originally had
document.addEventListener("DOMContentLoaded"...
and was linking to the JS file in the head of the HTML document.
The result was: some parts of another js script weren't working.
I then replaced 'DOMContentLoaded' with 'load' and it started working!
The weird thing now is:
When I move the link to the JS file to the footer of the HTML document, then and only then it actually works with 'DOMContentLoaded'. But that's very weird isn't it?
I have no idea what's going on! Any pointers?
Original post:
There is a big mystery I'm trying to unravel. While this code is working perfectly:
(function () {
"use strict";
var state = document.getElementById("s-state");
var btnEstimate = document.getElementById("btn-estimate");
// document.addEventListener("load", function() {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
state.addEventListener("change", function () {
if (state.value === "") {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
} else {
btnEstimate.removeAttribute("disabled");
btnEstimate.value = "Estimate Total";
}
});
// }, false);
})();
the following code does NOT work:
(function () {
"use strict";
var state = document.getElementById("s-state");
var btnEstimate = document.getElementById("btn-estimate");
document.addEventListener("load", function() {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
state.addEventListener("change", function () {
if (state.value === "") {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
} else {
btnEstimate.removeAttribute("disabled");
btnEstimate.value = "Estimate Total";
}
});
}, false);
})();
So, the big question is WHY??? Why is wrapping the code around in this:
document.addEventListener("load", function() {
}, false);
prevent it from working? That doesn't make any sense, does it? First, I thought the problem was me using 'DOMContentLoaded' but nope. Using 'load' produces the same result: non-working code.
Big mystery!
Here's the snippet with the code I actually had originally:
(function () {
"use strict";
var state = document.getElementById("s-state");
var btnEstimate = document.getElementById("btn-estimate");
document.addEventListener("load", function() {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
state.addEventListener("change", function () {
if (state.value === "") {
btnEstimate.setAttribute("disabled", "disabled");
btnEstimate.value = "Please select your state first!";
} else {
btnEstimate.removeAttribute("disabled");
btnEstimate.value = "Estimate Total";
}
});
}, false);
})();
<div class="group state">
<label for="s-state">State (required):</label>
<select id="s-state" required>
<option value="">- select -</option>
<option value="CA">California</option>
<option value="IL">Illinois</option>
<option value="NH">New Hampshire</option>
<option value="PA">Pennsylvania</option>
</select>
</div>
<p>
<label for="btn-estimate">Click to estimate:</label>
<br>
<input type="submit" value="Estimate Total" id="btn-estimate">
</p>