html - JavaScript append string with element(s) -


i'm trying use more , more native javascript code in stead of jquery. if want append string html elements in classified elements jquery it's easy:

<div class='destination'></div> <div class='destination'></div>  var selector = "class='svg_element'"; var svgelement = "<svg " + selector + " ></svg>";  $(".destination").append(svgelement); 

fiddle

how can short code in native javascript (without using loop)?

i tried lot, example thoughts:

document.queryselectorall(".destination").appendchild(svgelement); 

maybe creating element first:

var svg = document.createelement("svg");

//works document.queryselector(".destination").appendchild(svg);  //doesn't works document.queryselectorall(".destination").appendchild(svg); 

array.prototype.foreach.call( document.queryselectorall(".destination"),     function (node) { node.appendchild(svg); } );   

now save later:

nodelist.prototype.append = function (child) {     array.prototype.foreach.call(this, function (parent) {         parent.appendchild(child);     });  };   

but don't that.

do instead:

var appendtonodelist = function (nodelist, child) {     array.prototype.foreach.call(nodelist, function (parent) {         parent.appendchild(child);     });  };  appendtonodelist(document.queryselectorall(".destination"), svg); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -