lisovaccaro
lisovaccaro

Reputation: 33956

Replace innerHTML of all divs with same class

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

How can I do this?

Upvotes: 12

Views: 36973

Answers (2)

MhdSyrwan
MhdSyrwan

Reputation: 1633

you can do this by jQuery this way

$("div.count").html("new content");

Upvotes: 12

Šime Vidas
Šime Vidas

Reputation: 185933

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 transform the NodeList into a regular array, so that I can call the forEach method on it.

Btw, careful with innerHTML. I personally would use a library (like jQuery) to clone the contents instead.

Upvotes: 21

Related Questions