user5632700
user5632700

Reputation:

Javascript for in setAttribute

At school my teacher changed my code to example down below and it's not working and I can't understand how it works and fix it.

function _$(e, attrs) {
    var el = document.createElement(e);
    for(a in attrs){
        el.setAttribute(a, attrs[a]);
    }
}

   var $taskMain = _$("section", { "class": "sdfjsf", "id": "taskId", "data-id-number": "sajfsaf"});

//and i have error
//template.js:80 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
    currentCollon.appendChild($taskMain);

Upvotes: 0

Views: 58

Answers (2)

遁地龙卷风
遁地龙卷风

Reputation: 40

You're missing a return el; at the end of your _$ function.

give the result as follows,The _$ function creates a tag

 <section class="sdfjsf" id="taskId",data-id-number:"sajfsaf"></section>

This sentence code

currentCollon.appendChild($taskMain);

Before execution,assume that currentCollon is body

<body> 
</body>

After execution

<body>
<section class="sdfjsf" id="taskId",data-id-number:"sajfsaf"></section>
</body>

Upvotes: -1

user94559
user94559

Reputation: 60143

I think you're missing a return el; at the end of your _$ function.

Upvotes: 2

Related Questions