1

I am trying to get top and left attributes from element on page with jquery but it's not working.

Can you tell me how it's possible?

Thanks for any response

HTML code:

<div style="position: absolute; left: 370px; top: 0px;">
    <img class="name" src="image.jpg"/>
</div>

JQuery code:

$('body').on('click', '.name', function(){
    top = $(this).parent().css('top');
    left = $(this).parent().css('left');
    alert("Top: " + top + ",Left: " + left);
});

JQuery response:

Top: [object Window],Left: 185px
2
  • 1
    Remember, window also has a top property :) A good reminder that you should use descriptive variable names and avoid global variables Commented Apr 11, 2014 at 11:39
  • Read javascripter.net/faq/reserved.htm Commented Apr 11, 2014 at 12:09

1 Answer 1

3
top = $(this).parent().css('top');

This is pulling back the readonly window.top property which cannot be assigned to. Either scope the top variable, or give it a more relevant name:

$('body').on('click', '.name', function(){
    var top = $(this).parent().css('top');
    var left = $(this).parent().css('left');
    alert("Top: " + top + ",Left: " + left);
});

jsFiddle

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

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.