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 Answers
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.
1 Comment
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
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
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!
jsfile in the Resource section is executing the executing therating()function before. Check this fiddle out to see the problem. jsfiddle.net/Hg2xF