'Firefox JS not running unless debugged
I have a simple piece of code, that works fine both in IE and Chrome, but doesn't work in Firefox unless debugged.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.7.1.js" > </script>
<script>
$(document).on("click", "#chb", function () {
log("triggered, disabled = " + chb.disabled)
});
$(document).on("click", "#go", go);
cnt = 0;
function log(msg) {
$("#log").append("<span>" + ++cnt + ". " + msg + "<br/></span>");
}
function go() {
if (chb.disabled) {
chb.disabled = null;
console.log("set to null");
}
else {
chb.disabled = true;
console.log("set to true");
}
fireEvent(chb, "click");
}
function fireEvent(element, event) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
console.log("Event fired");
}
</script>
<style>
</style>
</head>
<body>
<input type="checkbox" id="chb">
<input type="button" id="go" value="Go">
<div id="log" />
</body>
</html>
It seems as if Firefox is unable to fire event. Does anybody know what could be the issue?
Solution 1:[1]
Okay, so if anyone stumbles across this problem... The thing is it's a bug. There is a problem, somewhere in Firefox, that you can't simulate an event just after you've madeyour input, button, or whatever enabled again. Simply use a function, (e.g. in this case it'd be like this:
$(document).on("click", "#chb", logg);
function logg () {
log("triggered, disabled = " + chb.disabled)
}
/// some code here
function go() {
if (chb.disabled) {
chb.disabled = null;
console.log("set to null");
}
else {
chb.disabled = true;
console.log("set to true");
}
if(!chb.disabled)
logg()
}
Hopefully it helps
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tom |
