0

In the top part of my document I have this Jquery code:

// RATING 
$('.rateit').click(function(){
var position = $(this).position();
});

In the footer I have a ajax call:

  jQuery.ajax({
             url: frm.attr('action'), //your server side script
             data: frm.serialize(), //our data
             type: 'POST',
             success: function (data) {
            alert(position.left);
            },

The problem is that the alert box is not showing with the position. I have tested with alert('something'); and it works fine.

My real ajax call:

 jQuery.ajax({
     url: frm.attr('action'), //your server side script
     data: frm.serialize(), //our data
     type: 'POST',
     success: function (data) {
    $('.warning').fadeIn(500).css({display: 'block',
        position: 'absolute',
        left: position.left + 50,
        top: position.top - 25
    }).append('asdasdsadsad'),
    $(ri).next('.ratingcount').html('asdasdasd')
    },
     error: function (jxhr, msg, err) {
         alert(msg); //something went wrong.
     }
 });
0

3 Answers 3

3

On click function, position is a local variable, so will only be visible in this scope.

Try this:

var position;

$('.rateit').click(function(){
    position = $(this).position();
});
Sign up to request clarification or add additional context in comments.

3 Comments

I have updated my question with my full ajax call. The position is not set for the warning div.
When this ajax is called? You need set position before Ajax call, not on click.
Or call ajax function on click .rateit element.
1

You declare position inside the anonymous function used as a click event handler. That means it won't be in scope when you try to alert it (you will get a ReferenceError).

You will need to declare position outside of the event handler function:

var position;
$('.rateit').click(function(){
    position = $(this).position();
});

However, you will still get an error if the click event handler hasn't been executed when your alert runs, because position will be undefined and therefore won't have a left property.

6 Comments

Still the position is not set in the ajax call.
I have updated my question with my real ajax call. The position is not set for the div warning.
@Railsbeginner That's because your position variable is set in response to an event from the user (clicking on an element), and your AJAX call is simply fired off as soon as the code is loaded.
what is a solution for that problem?
@Railsbeginner Either fire off the AJAX call in response to the same event or get the position in the success callback function for the AJAX call. Or both, I guess.
|
0

I's because your variable is defined inside $('.rateit').click(); try that:

var position;

$('.rateit').click(function(){
position = $(this).position();
});

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.