Any way to take the css value from another element and add it to others?

I'm using it 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 something wrong with my code, or is it just not possible?

It works until I add two values ​​together or when I use math.abs. The error console indicates that the margin on the left cannot be analyzed.

I have reasons for this, so please do not use the class! :) thanks.

+3
source share
2 answers

Functions replaceand Math.maxdo not work as your code would suggest. They return values:

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

and

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

necessary for the actual change of values. You will have to do the same with other variables.

+1

:

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

Source: https://habr.com/ru/post/1761979/