views:

28

answers:

2

I am using this right now...

if($('.current').hasClass('odd') && !$('.current').hasClass('even')) {
                        var oddMarL = $('.current img').css('margin-left'); 
                        var oddMarL2 = $('.touch').css('margin-left'); 
                        var oddMarT = $('.touch').css('margin-top'); 

                        oddMarL.replace('px', '');
                        oddMarL2.replace('px', '');
                        oddMarT.replace('px', '');

                        Math.abs(oddMarL,oddMarL2,oddMarT);

                        oddMarL = oddMarL + oddMarL2;

                        $('#zoom').css('margin-left',oddMarL);
                        $('#zoom').css('margin-top',oddMarT);
                    }

Is there something wrong with my code or is it just not possible?

It works until I add the 2 values together or when I use math.abs. In the error console it says margin-left cannot be parsed.

I have reasons for needing it this way so please don't recommend using a class! :) thanks.

+1  A: 

The replace and Math.max functions don't work the way your code seems to assume they would. They return values:

oddMarL = oddMarL.replace('px', '');

and

oddMarL = Math.abs(parseInt(oddMarL, 10));

are necessary to actually change the values. You'd have to do the same thing with the other variables.

Pointy
Also I'll note that you don't really have to strip the "px" if you're going to call `parseInt` because `parseInt` will just stop when it gets to the "p".
Pointy
Thank you! it works great now
cat
A: 

try this also:

$('#zoom').css('margin-left',oddMarL+'px');
$('#zoom').css('margin-top',oddMarT+'px');
Vaibhav Gupta
The browser will assume "px" anyway. The problem is that oddMarL is going to look like "12px15px" so adding yet another "px" can't help.
Pointy
the px gets automatically put there, it messes up when I add the px
cat