I have the following HTML
<h4 data-padding-top="100" data-padding-left="0" data-text-align="left" data-color="#ffffff">Promotion Two</h4>
I want to take the data attributes and assign them back as CSS styles to the H4. I have the following JS code but the css function call gives an error (I don't want to simply add style=""; in the HTML the client isn't comfortable):
var H4obj = {},
$thisH4;
function css(el, styles) {
console.log(styles);
for (var property in styles)
el.style[property] = styles[property];
}
$('h4').each(function(index, el) {
$thisH4 = $(this);
var el = $thisH4;
data = $thisH4.data();
$.each(data, function(key, val){
H4obj['data-' + key] = val;
});
$.each(H4obj, function(index, value){
css($thisH4, {index:value});
});
});
Am I over-killing this, is there a simpler way to do it ?
Any help is much appreciated