How to get just numeric part of CSS property with jQuery?
I need to do a numeric calculation based on CSS properties. However, when I use this to get info:
$(this).css('marginBottom')
it returns the value '10px'. Is there a trick to just getting the number part of the value no matter whether it is px
or %
or em
or whatever?
Solution 1:
parseInt($(this).css('marginBottom'), 10);
parseInt
will automatically ignore the units.
For example:
var marginBottom = "10px";
marginBottom = parseInt(marginBottom, 10);
alert(marginBottom); // alerts: 10
Solution 2:
This will clean up all non-digits, non-dots, and not-minus-sign from the string:
$(this).css('marginBottom').replace(/[^-\d\.]/g, '');
UPDATED for negative values