Skip to content

Conversation

@lloydlobo
Copy link
Contributor

@lloydlobo lloydlobo commented Mar 4, 2024

Description

Please describe what changes you made, and why you feel they are necessary. Make sure to include
code examples, where applicable.

What

Update the .then() handler in the onClick attribute to use an arrow function instead of a traditional function(arg) expression.

Why

The window scope was being accessed instead of the intended button.
This fixes the this binding within the lexical scope, ensuring that this is bound to the button instead of the window and resolving the issue.

Corresponding issue: #2257

Testing

Please explain how you tested this change manually, and, if applicable, what new tests you added. If
you're making a change to just the website, you can omit this section.

Preview

The GIF demonstrates that clicking the button logs this to the console. The change ensures correct this binding, logging the specific button instead of the window.

if (result.isConfirmed) {
    console.log(this);
    htmx.trigger(this, 'confirmed');
}

Animation_htmx_arrow_fn

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded
 Update the `.then()` handler in the `onClick` attribute to use an arrow function instead of a traditional `function(arg)` expression. 

This fixes the `this` binding within the lexical scope, ensuring that `this` is bound to the `button` instead of the `window` and resolving the issue.

Fixes/Closes issue  bigskysoftware#2257
@Telroshan Telroshan added the documentation Improvements or additions to documentation label Mar 17, 2024
Copy link
Collaborator

@alexpetros alexpetros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thank you! Appreciate the testing detail as well.

@alexpetros alexpetros merged commit b176784 into bigskysoftware:master Apr 25, 2024
@lloydlobo
Copy link
Contributor Author

Excellent, thank you! Appreciate the testing detail as well.

Yes, I find visual examples convey changes more effectively in this case. Thank you for merging the changes. I admire the effort being put behind htmx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

3 participants