1

I have to create multiple instances of the same css class to run this block of code which displays user comments and ratings.(In ratings, I set width of lit stars according to comment's rating) So I have to create as many instances of css class as there are comments.

<script>
var wid=<?=$comment->rating?>*20; //20 is the width of 1-star, comment->rating 
                                      //gives me rating of that particular comment.   
jQuery(function($){
$(".rating-star").css('width',wid);  //setting width according to calculated wid.
});
</script>

As there is only one rating-star class, all comments are getting the same star rating. How do I create as many instances of rating-star class as there are comments? Thanks in advance! Great community!

4
  • 4
    Why not just set the width property on the rating stars directly? If you have 100 comments, that means you're going to run 100 CSS selectors for something that could have been done along with HTML Commented Mar 6, 2013 at 19:11
  • @ColinMorelli How to set the width property directly? Dint catch you there. Commented Mar 6, 2013 at 19:14
  • 1
    When you print the stars: <div class="rating-star" style="width: <?=$comment->rating * 20?>px"></div> Commented Mar 6, 2013 at 19:15
  • @ColinMorelli Ah.. that's great! Didn't think in that way! Thanks for the tip! Commented Mar 6, 2013 at 19:19

2 Answers 2

3

Another way of doing this is by using data attributes and echo out the rating in there.

Working DEMO on jsFiddle.

So and example HTML item would look something like this:

<li>
    <p>item one</p>
    <span class="rating-star" data-rating="1"></span>
</li>

And your JS function:

(function () {

    // cache the selector
    $ratings = $('.rating-star');

    // run foreach on selector
    $ratings.each(function (index) {

        // variables
        var el = $(this),
            rating = el.data('rating'),
            width = rating * 20;

        // apply width
        el.css('width', width + "px");

    });

})();

And your CSS could use a star image, with background-repeat.

Sign up to request clarification or add additional context in comments.

Comments

2

You actually don't need to create as many classes as you have comments.

Here's what I mean, and there are a few ways to do this. Here are two:

Create .rating1 through .rating5 for instance, and as you go through and create the element ordinarily just given .rating-star, use this for the class:

class="rating-star rating<? $comment->rating $>"

Then in CSS:

.rating1 { width: 20px; }
.rating2 { width: 40px; }

..etc

Or... less awesome / slower way:

Where you have class='rating-star' right now, also set rating='rating $>' as a custom tag. Then:

$(".rating-star[rating]").each(function(){ 
    $(this).addClass("rating"+$(this).attr("rating") 
})

...with the same CSS as above.

1 Comment

Setting style="width: ?" directly is more data for the browser to download than my first example. If you just set another class along with the one you already do, you'll be efficient as well as accurate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.