How to delete "px" from 245px

use

var size = parseInt('245px', 10);

where 10 is the radix defining parseInt is parsing to a decimal value

use parseInt but don't use parseInt without a radix

The parseInt() function parses a string and returns an integer.

The signature is parseInt(string, radix)

The second argument forces parseInt to use a base ten numbering system.

  • The default input type for ParseInt() is decimal (base 10).
  • If the number begins in "0", it is assumed to be octal (base 8).
  • If it begins in "0x", it is assumed to be hexadecimal

why? if $(this).attr('num') would be "08" parsInt without a radix would become 0


This does exactly what you ask: remove last two chars of a string:

s.substr(0, s.length-2);

To convert 245px in 245 just run:

parseInt('245px', 10);

It retains only leading numbers and discards all the rest.


To convert a pixel value without the "px" at the end. use parseFloat.

parseFloat('245px'); // returns 245      

Note: If you use parseInt, the value will be correct if the value is an integer. If the value is a decimal one like 245.50px, then the value will be rounded to 245.