question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trigger Javascript function when component has loaded

See original GitHub issue

I’ll try to explain the use case as much as I can.

Using Sprig, I’ve ‘upgraded’ a page that used to build a bunch of (hidden) data in table rows on a page, and when a user clicked a button, a JavaScript function would grab the data and spit out a CSV file to download.

The data is coming from Craft Commerce Orders which (currently) is several thousand rows, and is likely to increase - this cause the page to take some time to fully load.

Sprig to the rescue! I’ve replaced the rows to use a component instead. Now the hidden data isn’t fetched until the button is clicked - making the page load in seconds. The problem I have is when the button is clicked, the component updates with the hidden data (this works), but when the data is captured I want the JS function to be called. I wasn’t sure which HTMX trigger would be used in this instance?

Pseudo code is as follows:

show the row of text 
show a button to download with a s-vars of download=true

if download=true
  build the hidden data
  trigger the download
endif

I assumed sticking some {% js %} tags in the component would mean when the component re-loads (with download set to true) the JS will be executed, but it’s not.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
bencrokercommented, Oct 26, 2020

Yeah that makes sense, you want the button to re-render the entire component, not just itself.

0reactions
jamiematrixcommented, Oct 26, 2020

Think I have it. I’ve taken out the s-* attributes from the button after ‘sprig’ attribute and the JS is being called once. I guess having those in there are making the component reload/change too?

I’m now left with: <button id="festibutton" class="button" sprig> That’s the only change to the component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: how can I call a function when a component has loaded?
I'm trying to use this property to call a function when the component has loaded but I don't think I'm accessing it correctly....
Read more >
How to run a function when the page is loaded in JavaScript
Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs...
Read more >
How do I call a JavaScript function on page load?
The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As...
Read more >
Page: DOMContentLoaded, load, beforeunload, unload
Page load events: The DOMContentLoaded event triggers on document when the DOM is ready. We can apply JavaScript to elements at this stage....
Read more >
How to Make JavaScript Execute After Page Load - W3docs
The Window.onload Function ... The onload property is an event handler that processes load events on a Window, XMLHttpRequest, HTML <img> element, etc....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found