I want to create a bunch of buttons in the html body based on an array stored in Javascript. Here is my code:
<!DOCTYPE>
<html>
<head>
<script>
var listBrand =['LEXUS','AUDI','MAYBACK','FERRARI','TOYOTA'];
//the array
function printBtn() {
for (var i = 0; i < listBrand.length; i++) {
var btn = document.createElement("button");
var t = document.createTextNode(listBrand[i]);
btn.appendChild(t);
document.body.appendChild(btn);
}
}
</script>
</head>
<body>
<div onload="printBtn();">
</div>
</body>
</html>
I want to have all 5 buttons LEXUS, AUDI, MAYBACK, FERRARI, and TOYOTA show up in the body when the page loads, but the buttons fail to appear.
Is there anything wrong with my code? Any help and/or explanations are appreciated. Thank you.
<body onload="printBtn();">and you are good to goonloadondiv, see my answer.