So I have this code here that I want to use to dynamically make elements, but it does not work:
make = {};
elements = ['a', 'div', 'span', 'form', 'h1', 'h2', 'h3', 'h4'];
elements.forEach(function(element){
make[element] = function() {
document.createElement(element);
}
})
However, when I do:
var h1 = make.h1();
I get undefined... Can anyone explain why the element param I am passing to the createElement function doesn't work? I inspected this to dry and debug and I noticed that the make object has all the correct properties and corresponding functions, however the createElement function doesn't seem to retain the correct value of the element I am tying to pass.
DISCLAIMER: I know there are tons of libraries that I can use but I am doing this for learning and understanding purposes. Thank you to anyone who takes some time to explain this to me. :]