my code below which using each() actually can work nicely, but is it use for to loop is better than to use each() ? use for to loop will reduce the timeload compare with each(); ? because in future i still will add more for the data type.. means not only c1,c2,still will have more type is coming,
my html ,
<div class ="box red" data-type="c1" data-title="c1 content" id="cp-1">
<div class= "title">c1 content</div>
</div>
<div class ="box green" data-type="c1" data-title="c2 content" id="cp-2">
<div class= "title">c2 content</div>
</div>
javascript :
$(document).ready(function() {
var cp = $('.box');
// Unique Id for each
var idCp = 0;
for (var i = 0; i < cp.length; i++)
{
idCp++;
cp[i].id = "cp-" + idCp;
}
cp.each(function() {
var cp = $(this);
var cpTitle = cp.attr("data-title") + "";
// different Data type
if (cp.data('type') == "c1")
{
cp.addClass('red').css(
{
"background-color" : "red",
"padding": "20px",
"display": "table"}
);
cp.append('<div class="title">' + cpTitle + '</div>');
}
else if (cp.data('type') == "c2")
{
cp.addClass('green').css(
{
"background-color" : "green",
"padding": "20px",
"display": "table"}
);
cp.append('<div class="title">'+ cpTitle + '</div>');
} else {
return false;
}
});
});