From 1391cf0e4b31c315f75fec82054c1ea7e768248c Mon Sep 17 00:00:00 2001 From: nyash Date: Fri, 14 Aug 2020 16:38:18 +0200 Subject: [PATCH 1/3] add "find" selector option to hx-target attribute --- src/htmx.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/htmx.js b/src/htmx.js index 7a5478b5..fe5e7214 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -321,6 +321,8 @@ return (function () { return explicitTarget; } else if (targetStr.indexOf("closest ") === 0) { return closest(elt, targetStr.substr(8)); + } else if (targetStr.indexOf("find ") === 0) { + return find(elt, targetStr.substr(5)); } else { return getDocument().querySelector(targetStr); } From df971a3667586310e0e80415f46d8a2d90b3f1a7 Mon Sep 17 00:00:00 2001 From: nyash Date: Fri, 14 Aug 2020 16:43:27 +0200 Subject: [PATCH 2/3] update hx-target docs --- www/attributes/hx-target.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/www/attributes/hx-target.md b/www/attributes/hx-target.md index dee4398e..1bec723b 100644 --- a/www/attributes/hx-target.md +++ b/www/attributes/hx-target.md @@ -12,6 +12,8 @@ request. The value of this attribute can be: * `this` which indicates that the element that the `hx-target` attribute is on is the target * `closest ` which will find the closest parent ancestor that matches the given CSS selector. (e.g. `closest tr` will target the closest table row to the element) + * `find ` which will find the first child descendant element that matches the given CSS selector. + (e.g `find tr` will target the first child descendant row to the element) Here is an example that targets a div: From 7e35a2ac413b7598b5c73f34a049497a5d95ca5e Mon Sep 17 00:00:00 2001 From: nyash Date: Mon, 31 Aug 2020 10:34:08 +0200 Subject: [PATCH 3/3] add hx-target find selector test --- test/attributes/hx-target.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/attributes/hx-target.js b/test/attributes/hx-target.js index d90c2db3..5969c728 100644 --- a/test/attributes/hx-target.js +++ b/test/attributes/hx-target.js @@ -47,6 +47,18 @@ describe("hx-target attribute", function(){ this.server.respond(); div1.innerHTML.should.equal("Clicked!"); }); + + it('targets a `find` element properly', function() + { + this.server.respondWith("GET", "/test", "Clicked!"); + var div1 = make('
Click Me!
') + div1.click(); + this.server.respond(); + var span1 = byId("s1") + var span2 = byId("s2") + span1.innerHTML.should.equal("Clicked!"); + span2.innerHTML.should.equal(""); + }); it('targets an inner element properly', function() {