Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - transform `elem.style.left` into integer?

Tags:

javascript

I need to make this comparison:

if (x < siteA.style.left || x > siteA.style.left + land.width() ) {

however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?

I tried alert(parseInt(siteA.style.left)) and it returned NaN however alert(siteA.style.left) returned 30px, 60px etc.

like image 420
lisovaccaro Avatar asked Jun 21 '26 12:06

lisovaccaro


2 Answers

Use parseInt:

var leftPos = parseInt(siteA.style.left, 10);

where 10 is the base, good practice to always specify it.

like image 156
Sarfraz Avatar answered Jun 24 '26 03:06

Sarfraz


Just use parseInt().

like image 22
stewe Avatar answered Jun 24 '26 03:06

stewe