0

So, this is what I thought would work:

Math.max($j.map($j.makeArray($j(".rl_shell")), function(val, i){
    $j(val).width();
}));

breakdown:

$j.isArray($j.makeArray($j(".rl_shell"))) 

returns true

and when I do just

$j.map($j.makeArray($j(".rl_shell")), function(val, i){
        $j(val).width();
    })

I get an empty array ([]) as a result.

$j(".rl_shell")

returns [div, div]

so, I'm guessing, that I'm using map wrong, but I used the syntax here: http://api.jquery.com/jQuery.map/ so I'm not exactly sure what the deal is.

EDIT:

as per a couple of the answer's suggestions, I've come up with this:

$j.map($j.makeArray($j(".rl_shell")), function(val, i){
    return $j(val).width();
});

which returns [0,950]

but when I add Math.max around it, I get NaN =/

3 Answers 3

4

Are you wanting to get the widest of all selected elements?

Math.max.apply(Math, $('selector').map(function(){ 
    return $(this).width(); 
}).get());
Sign up to request clarification or add additional context in comments.

3 Comments

I've come across an issue, I've updated my question to reflect the new problem.
why add the .get() at the end?
.get converts jQuery object collection into native array. You have to provide an array to the .apply. So Math.max([0,950]) returns NaN while Math.max.apply(Math, [0, 950]) is equivalent to Math.max(0, 950) and returns 950.
2

You just forget return from map callback:

Math.max($j.map($j.makeArray($j(".rl_shell")), function(val, i){
    return $j(val).width();
}));

2 Comments

bah, too used to ruby not requiring return o.o
I've come across an issue, I've updated my question to reflect the new problem.
2

Besides you missed the return of the map callback,

You should use Math.max.apply(window, arr) to get the max value of an array.

Pass an array directly to Math.max will return NaN.

The code will be like below:

Math.max.apply(window, $.map($('.rl_shell'), function() {return $(this).width();}));

1 Comment

oh hey, that's exactly what I just ran into. =D

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.