I know jQuery has a helper method for parsing unit strings into numbers. What is the jQuery method to do this?
var a = "20px"; var b = 20; var c = $.parseMethod(a) + b;
Use parseInt() method which takes string as first argument and return the Integer value.
parseInt() # The parseInt() method converts a string into an integer (a whole number). It accepts two arguments. The first argument is the string to convert. The second argument is called the radix .
You're looking for the parseInt() function. var left1 = "40px"; var left2 = "60px"; // Add the integer values of the left values together var leftTotal = parseInt( left1, 10 ) + parseInt( left2, 10 ) + "px"; Also worth investigating is the parseFloat() method.
No jQuery required for this, Plain Ol' JS (tm) will do ya,
parseInt(a, 10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With