5

I'm struggling with what is probably a very simple bit of jQuery

I have html like this:

<div class="star-rating" data-star-rating="5.0"></div>
<div class="star-rating" data-star-rating="2.0"></div>

I have some javascript which needs to do something based on the star rating of each of these elements and currently looks like this:

$('.star-rating').jRate({
    startColor : '#ccc',
    endColor : '#ccc',
    readOnly : true,
    rating : <value of data-star-rating>
});

I want to replace <value of data-star-rating> with the value of the data attribute relating to the element currently being processed

I thought this would work $(this).data('starRating') but it doesn't seem to

How can I access the value of the data attribute in this situation?

3
  • Have you tried $(this).data('star-rating')? Commented Mar 18, 2016 at 13:02
  • what does console.log((".star-rating").data('star-rating')) return? You dont even loop through elements, which one should it get ? 5.0 or 2.0 ? Commented Mar 18, 2016 at 13:12
  • @ksno it returns whatever the 1st one is... not really relevant though Commented Mar 18, 2016 at 14:07

5 Answers 5

5

You can use this too :

$(this).data('star-rating');

EDIT

After reading again the question. Comments are right, you should iterate through .star-rating array to apply the jRate to each element, sorry for my misunderstanding.

$('.star-rating').each(function () {       
    $(this).jRate({
        startColor: '#ccc',
        endColor: '#ccc',
        readOnly: true,
        rating: $(this).data('star-rating')
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.
I don't know why this has 5 upvotes as this doesn't answer the question? Why $(this).data('starRating'); has not worked as this is absolutely valid.
Just saying, after recent edit this looks exactly like my answer
4

$(this) doesn't refer to the element on which the jRate function is being called.

You can use the selector if there is only a single element having that class

$('.star-rating').jRate({
    startColor : '#ccc',
    endColor : '#ccc',
    readOnly : true,
    rating : $('.star-rating').data('star-rating')
});

For multiple elements:

Iterate over all the elements having the class star-rating and attach the plugin jRate individually with the rating value of the respective element.

$('.star-rating').each(function () {
    $(this).jRate({
        startColor: '#ccc',
        endColor: '#ccc',
        readOnly: true,
        rating: $(this).data('star-rating')
    });
});

JSFiddle DemoDidn't find CDN link of that plugin, so added the minified code in JavaScript pane itself

Comments

2

As there are more than one elements having class "star-rating" so you will need to loop through the elemets and forEach loop will make current iterating element accessible into the loop so you can use that element. And apply the JRate.

 $('.star-rating').forEach(function(dateRating){
  $(dateRating).jRate({
  startColor : '#ccc',
  endColor : '#ccc',
  readOnly : true,
  rating :  $(dateRating).attr("data-star-rating")
 });
});

2 Comments

While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. Thanks!
Added some description. I think this is so generic code anyone can understand If using JavaScript and JQuery.
1

You must use this:

$(this).attr('data-star-rating');

1 Comment

That's not true about data(). It will and does return HTML data attributes.
0

$(this).data('star-rating') will work if you return it from a function.

$('.star-rating').jRate({
    startColor : '#ccc',
    endColor : '#ccc',
    readOnly : true,
    rating : function () { return $(this).data('star-rating'); }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.