Window.Onload not working on function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
3rd Apr 2022, 10:59 PM
Martins Michael
Martins Michael - avatar
4 Answers
+ 6
Icon Mikky When you call function from a html event then no need to write function inside window.onload function because when event will be occur then function will be call. You have 2 button or more button doesn't matter.
4th Apr 2022, 1:35 AM
A͢J
A͢J - avatar
+ 3
When document is loaded, your javascript code will run, then when you try to click on button it will search for this function and it wont find it. We dont use window onload in such way, if you will add event listener from javascript then you need to add this inside window.onload. window.onload allow you to access elements after they are rendered to screen (in your code you dont need this), same as your script is placed at bottom of html page, right before </body> Check this code to understand better how it works: https://code.sololearn.com/WH0xv85NMI89/?ref=app
3rd Apr 2022, 11:17 PM
PanicS
PanicS - avatar
+ 1
Somehow tricky but thanks What if i have two button ?
4th Apr 2022, 12:45 AM
Martins Michael
Martins Michael - avatar
+ 1
you assign different variables to your buttons and add corresponding event listeners for each.
4th Apr 2022, 6:11 AM
Bob_Li
Bob_Li - avatar