Reputation: 172
Why is this not working? I'm trying to add a clas so I can add padding to the default Magento CMS image styles - big thanks in advance. Ignore the j - it has to be there
if ($j('.cms-page-view .std img').css('float') == 'left') {
$j('.cms-page-view .std img').addClass('img-left');
}
else if ($j('.cms-page-view .std img').css('float') == 'right') {
$j('.cms-page-view .std img').addClass('img-right');
}
else {
$j('.cms-page-view .std img').addClass('img-normal');
}
Upvotes: 1
Views: 81
Reputation: 657
As Satpal said you should use for each loop to iterate through each element because class selector will return list of elements
$j('.cms-page-view .std img').forEach(function(item){
if ($j(item).css('float') == 'left') {
$j(item).addClass('img-left');
}
else if (item).css('float') == 'right') {
$j(item).addClass('img-right');
}
else {
$j(item).addClass('img-normal');
}
}
);
Upvotes: 2
Reputation: 133403
You should use .each()
add the classes in each elements context
$j('.cms-page-view .std img').each(function(){
var elem = $(this); //Here this refers to element in an iteration
if (elem.css('float') == 'left') {
elem.addClass('img-left');
}
else if (elem.css('float') == 'right') {
elem.addClass('img-right');
}
else {
elem.addClass('img-normal');
}
})
Upvotes: 2