diff --git a/src/htmx.js b/src/htmx.js
index 0d8c0cfb7..6dbd9774d 100644
--- a/src/htmx.js
+++ b/src/htmx.js
@@ -1950,7 +1950,9 @@ return (function () {
function addHxOnEventHandler(elt, eventName, code) {
var nodeData = getInternalData(elt);
- nodeData.onHandlers = [];
+ if (!Array.isArray(nodeData.onHandlers)) {
+ nodeData.onHandlers = [];
+ }
var func;
var listener = function (e) {
return maybeEval(elt, function() {
diff --git a/test/attributes/hx-on-wildcard.js b/test/attributes/hx-on-wildcard.js
index 6872fa563..b45bb085f 100644
--- a/test/attributes/hx-on-wildcard.js
+++ b/test/attributes/hx-on-wildcard.js
@@ -175,4 +175,22 @@ describe("hx-on:* attribute", function() {
delete window.foo;
delete window.bar;
});
+
+ it("cleans up all handlers when the DOM updates", function () {
+ // setup
+ window.foo = 0;
+ window.bar = 0;
+ var div = make("
Foo
");
+ make("Another Div
"); // sole purpose is to update the DOM
+
+ // check there is just one handler against each event
+ htmx.trigger(div, "increment-foo");
+ htmx.trigger(div, "increment-bar");
+ window.foo.should.equal(1);
+ window.bar.should.equal(1);
+
+ // teardown
+ delete window.foo;
+ delete window.bar;
+ });
});
diff --git a/test/attributes/hx-on.js b/test/attributes/hx-on.js
index d03348ce4..51b4a13b0 100644
--- a/test/attributes/hx-on.js
+++ b/test/attributes/hx-on.js
@@ -171,4 +171,22 @@ describe("hx-on attribute", function() {
delete window.foo;
delete window.bar;
});
+
+ it("cleans up all handlers when the DOM updates", function () {
+ // setup
+ window.foo = 0;
+ window.bar = 0;
+ var div = make("Foo
");
+ make("Another Div
"); // sole purpose is to update the DOM
+
+ // check there is just one handler against each event
+ htmx.trigger(div, "increment-foo");
+ htmx.trigger(div, "increment-bar");
+ window.foo.should.equal(1);
+ window.bar.should.equal(1);
+
+ // teardown
+ delete window.foo;
+ delete window.bar;
+ });
});