Reputation: 557
So I have this code:
window.onload = make_buttons ('"calc"');
function make_buttons (id) {
console.log (id);
var input = document.createElement("INPUT");
document.getElementById(id).appendChild(id.input);
for (var i = 0;i < 10; i++){
var btn = document.createElement ("BUTTON");
var t = document.createTextNode (i);
btn.appendChild(t);
document.getElementById(id).appendChild(id.btn);
}
};
Basicly I insert the id of the div element in the make_buttons ()
function and then it should create everything that I ask it to create for me.
But for some reason this method throws me an error which says that getElementById
is given a NULL value. The console.log (id)
shows that id is "calc" getElementByID
should have "calc" inside of the (). Why is it so?
Also when I have created all thoes buttons, can I just add a .onclick=
after the .append.Child()
to make it have a on click event also?
Upvotes: 1
Views: 78
Reputation: 382102
1) The console always display quotes for strings, that doesn't mean that your string contains quotes. Your id is calc
, not "calc"
.
2) As noticed by Felix, you're assigning to window.onload
the result of the function instead of the function.
Replace
window.onload = make_buttons ('"calc"');
with
window.onload = function(){ make_buttons ('calc'); }
Upvotes: 3
Reputation: 9597
You haven't assigned the ID to the input you're creating. The input is in the document, but it doesn't have the ID you're passing at the time you're trying to access it.
Upvotes: 0
Reputation: 375
This might work for you, try using the body on load
<body onload="make_buttons('calc');">
</body>
Upvotes: 0
Reputation: 6349
problem is id.input
on document.getElementById(id).appendChild(id.input);
You are passing string as id. but appending id.input
in parent element which is incorrect.
Upvotes: 2