Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery / Javascript - How do I convert a pixel value (20px) to a number value (20)

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; 
like image 464
John Himmelman Avatar asked Jun 11 '10 15:06

John Himmelman


People also ask

How do you convert pixels to integers?

Use parseInt() method which takes string as first argument and return the Integer value.

Which method converts the string into a number in JavaScript?

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 .

How do you write PX in JavaScript?

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.


1 Answers

No jQuery required for this, Plain Ol' JS (tm) will do ya,

parseInt(a, 10); 
like image 83
Andy E Avatar answered Sep 20 '22 03:09

Andy E