Help plz. Why 1st work,2nd not. What's the problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help plz. Why 1st work,2nd not. What's the problem?

1st: function sayHello(name, age) { document.write( name + " is " + age + " years old."); } sayHello("John", 20); 2nd: function sayHello(name, age) { document.getElementById("show").innerHTML=( name + " is " + age + " years old."); } sayHello("John", 20);

26th Apr 2020, 5:58 AM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
2 Answers
+ 3
In Sololearn script it linked in head tag so it's likely that your html elements are not loaded, so you have to wait for it to load before calling that Javascript function. You have to use window.onload property. function sayHello(name, age) { document.getElementById("show").innerHTML=( name + " is " + age + " years old."); } window.onload = function() { sayHello("John", 20); }
26th Apr 2020, 6:04 AM
Raj Chhatrala
Raj Chhatrala - avatar
26th Apr 2020, 6:23 AM
Md Iftakher Hossain
Md Iftakher Hossain - avatar