trim "px" off the end of Jquery var

phil crowe picture phil crowe · Sep 2, 2010 · Viewed 24k times · Source

I have a variable which stores the css value of a margin. I want to remove the "px" from the end so that i just have the number to work with. How can i do this?

Answer

Alex Reitbort picture Alex Reitbort · Sep 2, 2010
var x = "1px";
var y = parseInt(x, 10); // specify radix to prevent unpredictable behavior