Replace innerHTML of all divs with the same class

I want to replace innerHTML of all divs with the class "count" with: items1.innerHTML.

How can i do this?

+4
source share
2 answers

Here you go:

var items = document.getElementById( 'items' ), divs = document.getElementsByClassName( 'count' ); [].slice.call( divs ).forEach(function ( div ) { div.innerHTML = items.innerHTML; }); 

Live demo: http://jsfiddle.net/MGqGe/

I use this [].slice.call( divs ) to convert a NodeList to a regular array so that I can call it forEach .

Btw, be careful with innerHTML . I personally would use a library (e.g. jQuery) to clone the content.

+9
source

you can do it with jQuery this way

 $("div.count").html("new content"); 
+3
source

All Articles