1

I'm working on a HTML project. Basically saving HTML nodes to a JavaScript's Object to append them inside a element. Here's my HTML, JavaScript and the error.

HTML

...
    <div id="holder"></div>
...

JavaScript

var _handler = {};
var _holder = document.getElementById('holder');
var some_example = [{"id":"item_1"}, {"id":"item_2"}]

function create(tag, id) { /*Created a DOMObject */
    var elem = document.createElement(tag);
    _handler[id] = elem;
}
function spawn() {
   for (var k in _handler) {
       _holder.appendChild(_handler[k]); //<----- Here's the error occurring, given at very last.
   }
}
function main() { /* Main function */
    for (var x=0;x < some_example.length;x++) {
         create('div', some_example.id);
    }
    spawn();
}

Sorry for that little complicated script. Anyways, all the work going great but, appendChild does the bobo given below:

Error

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

Hope, you guys have any idea. Thank You.

1

1 Answer 1

3

Make sure you use hasOwnProperty in your for loop.

for (var k in _handler) {
   if (_handler.hasOwnProperty(k)) {
       _holder.appendChild(_handler[k]); 
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.