views:

171

answers:

3

I'm using this plugin: http://www.fyneworks.com/jquery/star-rating/

Is it possible to put a click function inside a callback function like this?

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

The callback function will stop working once I add the click function. I need to do this because if the user clicks any stars, I can pick up the id of the star, but if the user clicks the cancel button I can't pick up the id of the cancel somehow.

Assuming you can use a click function inside the callback, would it still trigger it though? Because after you click it, it triggers the callback, would it still trigger my click function inside?

If I keep the click function separate things will still work, but then I'd have to repeat a bunch of code inside both functions.

+2  A: 

Try using the live handler

$(".myclass").live("click", function(){
  alert($(this).attr('id'));
});
halocursed
Cool, this works. However, sometimes I get multiple alerts for some reason after I rate things, even though it was only one click. Any idea why that would happen?
Roger
A: 

Instead of binding the click on the callback, have you considered using the .live(type, fn) event of jQuery?

http://docs.jquery.com/Events/live#typefn

Mike Robinson
+1  A: 

To prevent multiple actions from taking place when clicking, you need to destroy the previous event before adding a new one (otherwise they'll just stack on top of each other). In order to destroy (i.e. kill) an event, you need to:

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

Alternatively (and preferably), don't attach the click event from within the callback... do it separately.

$(".myclass").live("click", function(){
    alert($(this).attr('id'));
});
$('.auto-submit-star').rating()

Good luck with your project.

KyleFarris
Thanks, that's very helpful. The reason I place the click inside the callback function is after I get the id I will basically be repeating the same code inside each function so I was trying to reduce the repeated code.
Roger
Hmm, actually what's weird is that code that is placed outside the live click function is processed before the code in the live click function. Why is that?
Roger
@Roger: I think you're getting confused at what the `live()` method does. Basically what the first 3 lines of the second code example I gave does is (1) finds all elements with the class `myclass` and makes it so that whenever one of those elements is clicked, an alert box pops up with the id of the clicked element. It then repeats the same process every time another element with the class `myclass` is added to the DOM. So, in essence, as a user, you will not know that the `live()` method has been processed until you click on a matching element. Did that help?
KyleFarris
Ah I see. Probably not what I want to do then. :p Basically I need both of these functions to run the same ajax request, but I'd prefer do it in a way that wouldn't repeat the same code. No matter though I guess. Thanks for the help.
Roger