0

I am using this jQuery star rating plugin and I would like to get the name of the star that I clicked. The code can be fiddled here. How can I get the name of the element that is clicked?

4
  • 1
    try checking the view -> source of the page to check what is rendered corresponding to the stars for your plugin... Commented Apr 24, 2011 at 15:24
  • I can't get the callback to fire at all, whatever I place in it... Commented Apr 24, 2011 at 15:25
  • 2
    Your js file in the Resource section is executing the executing the rating() function before. Check this fiddle out to see the problem. jsfiddle.net/Hg2xF Commented Apr 24, 2011 at 15:29
  • I fixed the link tot he correct fiddle Commented Apr 24, 2011 at 15:32

4 Answers 4

1

Looks like that rating plugin creates it's own 'stars' to replace the ones you have in your original HTML, and they do not have the same name attribute.

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

1 Comment

so I have to use a different classname? the question is how do I get the name
0

With jQuery:

$(document).ready(function(){
     $('.star').click(function(){
          alert($(this).attr('name'));
     });
});

PROTIP: You should probably give each of your stars a unique ID so you actually know what is being clicked.

3 Comments

actually this is inside fancybox, so when I do that I get the fancy box name
Then manipulate it to descend into the children to find the element you are looking for?
Sorry but he was executing the function before and so it was not working.
0

there you go: http://jsfiddle.net/ezmilhouse/7mY7w/

$('.star-rating-control a').live('click.app',function() {
    var parent_classes = $(this).parent('div').attr('class');
    console.log(parent_classes);
    var arr = parent_classes.split(" ");
    var rater = arr[1];
    console.log( rater );
    var rating = $( "." + rater + ".star-rating-on" ).length;
    console.log(rating)
    alert( rater + " has a rating of " + rating  );

    // save in database
    var toBeSaved = rater + '-' + rating; // ex.: rater-1-4

    // or 
    var group = rater.split("-")[1];
    var stars = rating;

});

3 Comments

the stars actually don't have a name but the combination of 'rater' (group of stars) plus 'rating' (selected star) should help
I actually want to get the name.. as I will use this as an id to post into the db
you could save the selector: var toBeSaved = rater + '-' + rating; (rater-1-4)
0

Here is your solution. The script you included in the Resource Section was executing the function before, that's why it was rejecting the execution of the same function double time. Below is the link to your solution. See no changes at all and you can do whatever you want with the function variables value and link!

SOLUTION-1

SOLUTION-2

2 Comments

I have a fancybox with an I frame, your solution uses alert($(this).attr('name'));, in my case, it returns the fancybox name... how do I iterate down to the star?
Try this fiddle. jsfiddle.net/yFzgL (Scroll to left and click on any star you want!) Hope it helps!

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.