0

I am using jquery raty rating plugin and would like to grab the score on click and pass it to to the second function which will send it via ajax to php to insert in to database along with a feedback. can someone help me out?

 $(function() {
    $('.rate').raty({
    click: function(score, evt) {
    var rate = score;//this is the variable I want to pass
    },
    path     : '../img',
     size     : 24,
     half :true,
    cancel      : true,
    cancelOff : 'cancel-off-big.png',
    cancelOn  : 'cancel-on-big.png',
    starHalf : 'star-half-big.png',
    starOff  : 'star-off-big.png',
    starOn   : 'star-on-big.png',
    starOn   : 'star-on-big.png',
     score: function() {
    return $(this).attr('data-score');
    }
  });

  //the below function is in included js file
 $(function() {
$("#submit" ).click(function() {
   //how can I access the above var here? 
   if(rate == "")
{
    $(".error").html("Score is missing");
    }
    else{


$.ajax({
    //send data
    });
 }

}); });

2 Answers 2

3

Prefixing a variable with var makes it accessible only within that scope. By doing this, you're only making it available inside the click handler for .rate.

Instead, make the scope wider;

// define here, in a scope both functions can access!
var rate = null;

$(function () {
    $('.rate').raty({
        click: function (score, evt) {
            rate = score;
        },
        path: '../img',
        size: 24,
        half: true,
        cancel: true,
        cancelOff: 'cancel-off-big.png',
        cancelOn: 'cancel-on-big.png',
        starHalf: 'star-half-big.png',
        starOff: 'star-off-big.png',
        starOn: 'star-on-big.png',
        starOn: 'star-on-big.png',
        score: function () {
            return $(this).attr('data-score');
        }
    });

    //the below function is in included js file
    $(function () {
        $("#submit").click(function () {
            if (rate == null) {
                $(".error").html("Score is missing");
            } else {

            $.ajax({
                //send data
            });
        }
    });
});

You'll notice I've changed your rate == "" to rate == null; I've looked at the raty docs, and the only valid values I could find for the click handler is either a number, or null (in the event "cancel" is clicked). So the comparison against null will work for both the user not choosing a value, or the user clearing the value (if you have that setting enabled).

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

Comments

0

Variable rate is defined in callback function. If it's defined inside of function it means it's local variable and it's valid and exist only in this function. You must define this variable out of this function.

If you can't do so - becouse of external file with jQuery Raty code which you can't edit - so in such case - I think the only one possibility is to read DOM and take value of hidden input which is generated by jQuery Raty.

Raty code

But input doesn't fire change event when the value is programmatically changed so I have no idea how to detect that user clicked rating icon .......

Comments

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.