Search code examples
javascriptjquerywidtheach

Jquery width() through iteration?


I'm having a bit of trouble iterating and retrieving width() values.

$(".MyClass").each(
    function () {
        elemWidth = $(this).width();
    });

So for each "MyClass" element, I would like to find the width (some are different).

But the above code only produces the width() of the first instance of MyClass and returns 0's after that.

Any ideas on being able to obtain the width of every instance of MyClass?


Solution

  • Use map instead of each to simplify your code:

    $('.MyClass').map( function() {
        return $(this).width();
    });