javascript - How to add spans to every word in paragraph of text? -


how append span every word in particular document div ("text")? new using nodes in js. keep getting message,

"object has no method append child..."

what missing? code:

var y;  var words;   function get() {     y = document.getelementbyid("text").firstchild.nodevalue;        words = y.split(" ");       (var x = 0; x < 3; x++)     {         var newspan = document.createelement('span');          words[x].appendchild(newspan);       } } 

you need first fetch dom element want append words to,

elem = documents.getelementbyid("#some_container"); 

then append words

words.foreach(function(w) {   var new_span = document.createelement('<span>');  new_span.innerhtml = w;  elem.appendchild(new_span); }); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -