Set background with value of data attribute

user2413035 picture user2413035 · Jul 8, 2013 · Viewed 12.8k times · Source

I've 16 different section-tags. Every section-tag have a data- attribute to set a specific background-color for each section:

<section data-color="#A3D1B5">

Now I want to set this color as the background.

What i've already tried
In this question CSS values using HTML5 data attribute say the answer, it should be possible to set the color like background: attr(data-color); but this won't work for me...

I took a look at jQuery data() but I don't know how to set the background for all of the section-tags.

Any other solutions or tips how to handle this with jQuery data()?

Answer

A. Wolff picture A. Wolff · Jul 8, 2013

DEMO

$("section").css('background', function () { //or for code's consistency, i'd use background-color instead
    return $(this).data('color')
});