Reputation: 26489
I have a page with lots of comments.
The code structure is as follows:
+ <li id="li-comment-65" class="comment even thread-odd thread-alt depth-1">
<div class="comment-body">
<div id="comment-65">
<div class="comment-author vcard">
<img height="30" width="30" href="" alt="" />
<cite class="fn">keith</cite>
</div>
<div class="comment-meta commentmetadata">
<a href="#">June 16, 2009 at 10:21 pm</a>
</div>
<div id="rating-65" class="rating-class">Excellent</div>
<p>Hiya</p>
</div>
</div>
</li>
What I want to do is the following:
Is this hard to do?
Upvotes: 1
Views: 257
Reputation: 488354
This should do it:
$('div.rating-class').each(function() {
var value = $.trim($(this).text());
var src;
switch(value) {
case 'Excellent':
src = 'fivestars.png';
break;
case 'Very Good':
src = 'fourstars.png';
break;
...
}
$img = $('<img/>').attr('src', src);
$(this).html($img);
});
Even better would be to do something like this:
$('div.rating-class').each(function() {
var value = $.trim($(this).text()).replace(' ', '_').toLowerCase();
$(this).addClass(value);
});
And then have CSS classes like these:
div.rating-class.excellent {
background-image: url(fivestars.png);
text-indent: -1000px;
}
div.rating-class.very_good {
background-image: url(fourstars.png);
text-indent: -1000px;
}
...
Where the text-indent would hide the regular text you originally had there.
Upvotes: 3