Jquery share variable

Why is this not working? Tearing off my hearing ...

This fiddle was updated with the approved answer http://jsfiddle.net/jbreezy/pswdn2wb/

$(document).ready(function() { var marginFix = $('.marginFix').css('font-size'); var onethirdMargin = parseInt(marginFix / 3); $('.marginFix').css({"margin-top":marginFix}); $('.marginFix').css({"margin-bottom":onethirdMargin}); });

As you can see there, I just want the 'onethirdMargin' variable to be 1/3 of the other variable.

So, regardless of the fact that you have ever set the font size in css, the margin-top elements will be equal to the specified font size, and the edge of the field will be 1/3 of this automatically (If you apply the marginFix class to it). Currently, the bottom margin is still equal ... Any help is much appreciated!

+4
source share
1

var onethirdMargin = parseInt(marginFix / 3);

to

var onethirdMargin = parseInt(marginFix, 10) / 3;

parseint "24px", 24, 3.

Mozilla https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt ParseInt, 10, , , Rocket Hazmat .

+13

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


All Articles