1

I want to do something like:

<section id="{{flasklist[1]}}" onload="$('#exp{{flasklist[0]}}').click()">

But it has no effect. Separately, I know the action $('#exp{{flasklist[0]}}').click() works as intended, its just not being triggered. Im fairly limited, because the page and section are being called with window.open() so I can't follow that with anything. Is there a way to do this without restructuring the whole site?

2 Answers2

0

No. Only things with URLs get load events (with the onload attribute on the <body> element representing the load event for the whole document).

If you want to run some JS as soon as a chunk of HTML has been added to the DOM: Just put a script element after it.

<section id="{{flasklist[1]}}">
</section>
<script>
    $('#exp{{flasklist[0]}}').click()
</script>
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • I tried the approach, and it didnt work. Maybe its the way Im going to the section? You can see this live btw at justdogs.herokuapp.com and maybe that might help too. Here's the implementation of the suggested approach. ```
    ``` Which I tested in console, and the script works as intended, but its not being triggered. This is how Im opening the page, and section (in another js function called `gotoDog()`): ```window.open("/"+dog_pages[dog]+"#"+dog, "_self");```
    – Ian Rubenstein Apr 25 '20 at 16:21
  • @IanRubenstein — Duplicate: https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element The button doesn't exist in the HTML before the script so it doesn't exist in the DOM when the script runs. — (and why do you have a `
    ` with nothing in it except a `
    – Quentin Apr 25 '20 at 17:00
0

I would write a script tag inside the section, and place the js there to be called

<section><script>$('#exp{{flasklist[0]}}').click()</script></section>
domSurgeon
  • 91
  • 1
  • 2