Search code examples
jqueryvariablesoffset

Find .offset of each element with X class


I'd like to know how you can get the .offset of multiple elements with variables, the idea would be something like this:

var position = $(this).offset().top;
$(".number").text(position);

And that way, every element with the .number class would display its own particular offset, the problem is that this isn't working, it's undefined, and I can't seem to figure out how to do it.

Any help would be appreciated.

Thanks in advance!


Solution

  • You can use the function parameter of text() method:

    $(".number").text(function(){
       return $(this).offset().top; // or this.offsetTop (relative to parent node)
    });
    

    -jsFiddle-