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?

4
  • Why you expect {innerHTML:"Hi!"} in some mysterious way to change to <p>Hi!</p>??? Commented Jun 26, 2014 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. Commented Jun 26, 2014 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. Commented Jun 26, 2014 at 11:01
  • Just enumerate the properties of data and assign it to x. Commented Jun 26, 2014 at 11:02

1 Answer 1

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

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

1 Comment

No, that's not what I mean. The problem is at the function document.createElement which I created.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.