Javascript: parseInt() with trailing characters

Posted by chris_l on Stack Overflow See other posts from Stack Overflow or by chris_l
Published on 2010-05-14T10:38:21Z Indexed on 2010/05/14 10:44 UTC
Read the original article Hit count: 192

Filed under:
|

parseInt("7em", 10); returns 7 in all browsers I tested [*]. But can I rely on this?

The reason I ask is, that I want to perform some calculations based on em, like

/* elem1.style.top uses em units */
elem2.style.top = parseInt(elem1.style.top, 10) + 1 + "em";

I could do this with regular expressions, but parseInt is easier to use, and probably a bit faster. Or is there another solution (maybe using jQuery)?

[*] Tested so far on: IE 6, IE 8, Safari 4, Firefox 3.6, Opera 10.5

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery