0

I am modifying CSS attributes of a div in vanilla JS. When I grab the value, it's a string with px on the end, like '340px' so I can't just add a number directly. I end up doing this:

element.style.top = element.style.top.split("px")[0]+pixelsToAdd;

It works fine, but it feels hacky. Is there a more direct way to grab and set the value, or is this it?

1
  • 1
    parseInt(value) Commented Feb 3, 2017 at 14:27

1 Answer 1

3

Yes, you can use parseInt() to convert it to an integer:

var px = "302px";
console.log(parseInt(px));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.