Reputation: 2569
Why does this work
$(parent)[0].appendChild(el);
and this dont?
$(parent)[0].prepend(el);
Coming from:
makeSVG('.svg-global', 'g', {id:'setores'}, true);
makeSVG('#setores',x.name, x.data,false);
And the function itself:
function makeSVG(parent, tag, attrs,pre) {
var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
el.setAttribute(k, attrs[k]);
if(pre){
$(parent)[0].prepend(el);
}else{
$(parent)[0].appendChild(el);
}
}
Upvotes: 2
Views: 692
Reputation: 318182
Javascript has no prepend method, jQuery does
$(parent).eq(0).prepend(el);
and
$(parent).eq(0).append(el);
are jQuery methods, while appendChild
is a native javascript method.
When you use [0]
you get the first underlying native javascript element in the collection, and jQuery methods does not work on regular DOM nodes.
Unfortunately there is no prependChild
method for native nodes, you'd have to use insertBefore
parent.insertBefore(el, parent.firstChild);
Upvotes: 3