+ 2
I want to take name from user and convey birthday wishes..
4 odpowiedzi
+ 1
window.onload=function(){
var x =prompt("Enter your name");
var f=document.createTextNode(x);
document.getElementsByClassName("takefromuser")[0].appendChild(f);
}
You should read about how getElementsByClassName works ,it returns an array of all the classes with same name so to actually access the element you need to call it by index like [0] or [1] like you would do with an array
And window.onload function tells browser to load JavaScript only after html document is loaded otherwise undefined errors will be thrown
+ 1
👍
+ 1
Mustafha Ahmad Instead do it this way
window.onload=function(){
var x =prompt("Enter your name");
document.getElementsByClassName("takefromuser")[0].innerHTML=x;
}
0
Thank you