views:

653

answers:

3

Using this plugin: http://www.fyneworks.com/jquery/star-rating/#tab-Testing

I have a simple callback function that picks up the id from the radio buttons:

<input type="radio" class="auto-submit-star {split:2}" id="myid" value="1" />

$('.auto-submit-star').rating({ 
  callback: function(value, link){ 
   alert($(this).attr('id'));
  } 
});

This works fine, but if the user clicks on the cancel button, then it can't read the id of it.

In the js, I think the cancel button is added dynamically with:

control.cancel = $('<div class="rating-cancel"><a title="' + control.cancel + '">' + control.cancelValue + '</a></div>')

If I add an id to it like this:

control.cancel = $('<div class="rating-cancel"><a id="someid" title="' + control.cancel + '">' + control.cancelValue + '</a></div>')

How could I read the id? This would be undefined. I can set a class and use $('.myclass').attr('id') but I will have multiple ratings on one page so I'll need something similar to "this". Or is it possible for the cancel button to pick up the id of the corresponding radio buttons?

+1  A: 

If the id is undefined, then you know they clicked the cancel button. No need to set an id for it.

if (typeof $(this).attr('id') == 'undefined') {...}
James Skidmore
The reason I need to pick up the id is to pass it off to my database. There will also be more than one rating per page. So for example, if I have 3 ratings on one page, and the user clicks the cancel button on the 2nd rating, I would need to know that it was on the 2nd set and I would need that specific id to insert into one of my columns. Hopefully you understand what I mean.
Roger
A: 

Ok Roger - I found this because I was facing the same EXACT issue. Here is how I solved it for the time being. I do hope the plugin gets fixed in the future. My sense is that you don't need the issue resolved at this point, but it may help out other folks. Obviously, it relies on structure of the DOM elements, hence not a very elegant solution.

//RYAN OBEROI: A big hack, since the cancel callback does not have the appropriate information
name = $(this).parent().next().attr('name')

// click callback, as requested here: http://plugins.jquery.com/node/1655
if(control.callback) control.callback.apply(input[0], [input.val(), $('a', control.current)[0], name]);// callback event
Ryan Oberoi
A: 

Thanks Ryan! I added the click event to rating-cancel class which worked for me

jQuery(".rating-cancel").click(function() {
        var name = jQuery(this).parent().next().attr('name');
        if (name != "") {
           //added the custom code here to handle rating cancel event
        }
      });
Amit Jain