0

Here is my code :

document._createELement = document.createElement;
document.createElement = function(type, data){
    var x = document._createElement(type);
    x = data;

    return x;
}

And I tried this :

document.body.appendChild(document.createElement("p", {
    innerHTML: "Hi!"
}));

And I expect to see this in the HTML :

<p>Hi!</p>

What's the solution to complete the document.createElement function above?

Mark Walters
  • 12,060
  • 6
  • 33
  • 48
  • Why you expect `{innerHTML:"Hi!"}` in some mysterious way to change to `

    Hi!

    `???
    – Teemu Jun 26 '14 at 10:59
  • So now, I've got "Hi!" in `data` and I want to put `data` into `x` so that `x.innerHTML = data.innerHTML` but I must be able to add all kinds of attribute in `data` such as `style`, `className`, `id`, etc. – user3698272 Jun 26 '14 at 11:01
  • Yes, yes I did. That's why I'm asking. At first you assign a HTMLElement to `x`, then on the next line you assign an anonymous object to that same `x`. – Teemu Jun 26 '14 at 11:01
  • Just enumerate the properties of `data` and assign it to `x`. – Rob W Jun 26 '14 at 11:02

1 Answers1

0

If you are just wanting to create a p element and append to the body, this will do it

var p = document.createElement("p");
p.innerHTML = "Hello!";
document.body.appendChild(p);

EDIT

Missunderstood you mean this

function createEl(type, data) {
    var el = document.createElement(type);
    for ( var x in data ) {
      el[x] = data[x];
    };
    return el;
}

var p = createEl("p", { innerHTML : "Hi There!" });
document.body.appendChild(p);

JSFiddle

Mark Walters
  • 12,060
  • 6
  • 33
  • 48