For example, if I have my HTML code like this:
<div class="color-ff0000"></div>
Is it posible for jQuery to take anything after "color-"
and append it to class like this
color:ff0000
Basically to create dynamic classes?
I found what I need and here is the answer:
$("[class*='color'], [class*='bgcolor']").each(function() {
var elClasses = $(this).attr('class').split ( ' ' );
var elCur = $(this);
for (var index in elClasses) {
if (/^color-\w+$/.test(elClasses[index])) {
var classColor = elClasses[index].split ( '-' )[1];
elCur.css('color', '#' + classColor);
}
if (/^bgcolor-\w+$/.test(elClasses[index])) {
var classBgColor = elClasses[index].split ( '-' )[1];
elCur.css('background-color', '#' + classBgColor);
}
}
});
.height{
width:200px;
height:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="color-673AB7 height dfg sefg bgcolor-ff0000 "><h1>Banana Developer</h1></div>
<div class="color-eeeeee height dfg sefg bgcolor-212121 "><h1>Banana Developer</h1></div>
Also don't know why this question is marked as duplicate, the question is completely different from the one linked above. Also couldn't find this anywhere else on stackoverflow.