1
    var htmlComponent = [
                {
                    element : 'button',
                    text : "Addition"
                },
                {
                    element : 'h1',
                    text : "This is the heading"
                },
                {
                    element : 'p',
                    text : "This is the paragraph."
                }
            ];

    htmlComponent.forEach(function(item) {

          // Problem here

 document.body.appendChild(document.createElement(item.element).appendChild(document.createTextNode(item.text)));
    }

Actually I wanted to create an html element using DOM Object but this is not working. I mean my code is not working properly..

but when I changed something Like that:

htmlComponent.forEach(function(item) {
    var _element = document.createElement(item.element);
    var text = document.createTextNode(item.text);
   _element.appendChild(text);
    document.body.appendChild(_element);
}

Then the code is working.

Here the main question is why 2nd code is working and the 1st one is not working...... what is the problem in my code.

please Explain me........

2
  • 3
    because appendChild does not return parent node where addition was Commented Dec 13, 2017 at 9:47
  • You should visit this w3schools.com/jsref/met_document_createelement.asp Commented Dec 13, 2017 at 9:48

3 Answers 3

1

You are chaining the calls together like body.createElement().appendChild() where you shouldn't.

This works with createElement() because it returns the element you want to append to, but it doesn't work with appendChild() because that returns the child you just appended, which you are then appending again to the body.

This programming style is known as a "fluent" interface. It is supported by some libraries e.g. jQuery, but not by native Javascript DOM functions.

See https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

Sign up to request clarification or add additional context in comments.

Comments

1

Try as follows

appendChild does not return parent

var htmlComponent = [{
    element: 'button',
    text: "Addition"
  },
  {
    element: 'h1',
    text: "This is the heading"
  },
  {
    element: 'p',
    text: "This is the paragraph."
  }
];

htmlComponent.forEach(function(element) {

  var btn = document.createElement(element.element); 
  var t = document.createTextNode(element.text);
  btn.appendChild(t);
  document.body.appendChild(btn);

});

Comments

0

According to the documentation for appendChild:

The returned value is the appended child except when the given child is a DocumentFragment, in which case the empty DocumentFragment is returned.

You are appending a text node to the button, then trying to append the returned result to the body. This is the reason why you are not seeing the button being appended to the body.

If you break it down like this, it's easier to see what's going on:

document.body.appendChild(

   // createElement returns button
   document.createElement("button")

     // button.appendChild then returns the appended child (a text node)
     .appendChild(document.createTextNode("text"))
)

2 Comments

Thanx... I understand whats going on here may be Second replace the first correct if I am wrong..
Hm no, that would not work. I would actually go with the second approach you had. That made more sense.

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.