what's mistake 🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
3rd Jun 2021, 11:11 AM
Nirmal Choudhary
1 Answer
+ 2
if you want to put the js for an event handler as a string, you must put it as attribute of the element: <button onclick='document.getElementById("a").innerHTML="hello"'>click me</button> if you want to attach it at runtime (dynamically), you must be sure that the element was parsed at that time (in sololearn that's not the case in the js tab, wich is inserted as 'script' element at top of the 'head' part of the html), and you must assign the element reference property on{event} with a function, not a string... onload = () => { button = document.querySelector('button'); button.onclick = () => document.getElementById("a").innerHTML="hello"; }; (onload event here belong to the global object 'window', wich doesn't need to be explicitly mentioned) or similarly, by attaching a function to the targeted element through the addEventListener method (but you may discover this later ;P)
3rd Jun 2021, 11:54 AM
visph
visph - avatar