Search code examples
javascriptjqueryclass-attributes

Javascript: Printing class attributes


I got a class in css :

img {
    position: relative;
    left: 0;
    top: 0;
}  

and an img in the HTML body. I can move the img with my JQuery/Javascript code:

 $('img').animate({left: "-=20"}, 'fast');    

Now I want to print out the attribute 'left' from my img class. Any Suggestions how to do that? My window.alert($('img').left) does not work. Thanks


Solution

  • To change a certain css attribute, use

    $('foo').css('bar', '42');
    

    To access a certain css attribute, use

    $('foo').css('bar');
    

    Thus, to answer your question:

    window.alert($('img').css('left'));
    

    To learn more: http://api.jquery.com/css/