Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1103 by evaluating hx-vals with this referring to the element #3139

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3855,7 +3855,7 @@ var htmx = (function() {
}
let varsValues
if (evaluateValue) {
varsValues = maybeEval(elt, function() { return Function('return (' + str + ')')() }, {})
varsValues = maybeEval(elt, function() { return Function('return (' + str + ')').call(elt) }, {})
} else {
varsValues = parseJSON(str)
}
Expand Down
12 changes: 12 additions & 0 deletions test/attributes/hx-vals.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,16 @@ describe('hx-vals attribute', function() {
this.server.respond()
div.innerHTML.should.equal('Clicked!')
})

it('js: this refers to the element with the hx-vals attribute', function() {
this.server.respondWith('POST', '/vars', function(xhr) {
var params = getParameters(xhr)
params.i1.should.equal('test')
xhr.respond(200, {}, 'Clicked!')
})
var div = make('<div hx-post="/vars" hx-vals="javascript:{ ...this.dataset, }" data-i1="test"></div>')
div.click()
this.server.respond()
div.innerHTML.should.equal('Clicked!')
})
})
1 change: 1 addition & 0 deletions www/content/attributes/hx-vals.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ In this example, if `foo()` returns an object like `{name: "John", age: 30}`, bo
* `hx-vals` is inherited and can be placed on a parent element.
* A child declaration of a variable overrides a parent declaration.
* Input values with the same name will be overridden by variable declarations.
* When using `javascript:`, `this` refers to the element with the `hx-vals` attribute