we can assign the input value to a variable and then we're assingning that variable to an element's innerHTML, likewise why can't I insert text object into an element's innerHTML??
eg code:
x = document.getElementById("my_input"). value;
y = document.createElement("p");
y.innerHTML = x;
This code works but the below code isn't, why?
eg code:
x = document.getElementById("my_input"). value;
y = document.createElement("p");
z = document.createTextNode("x");
y.innerHTML = z;
I'm a beginner in js.. Correct me if I'm wrong..thanks!!
as you can read here: https://www.w3schools.com/jsref/prop_html_innerhtml.asp The innerHTML property can only be set to a string. Because
zis a DOM object then it wont work as you expect. To add a DOM object you must use theappendChildmethod. You can read more here: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChildIt all comes down to types. One propery is of type string so you should not pass an object to it.