7

Here's my code:

var frameWidth = 400;
var imageWidth = $('#inner-image').css('width');
var numberOfFrames = imageWidth/frameWidth;

How do I make "numberOfFrames" display as a quotient? I.E. process "frameWidth" and "imageWidth" as numbers, rather than objects?

Let me know if I need to explain myself more clearly. Thanks!

1 Answer 1

15

.css('width') is likely returning the value with px. You can use parseInt() to get only the number.

var frameWidth = 400;
var imageWidth = parseInt( $('#inner-image').css('width'), 10);
var numberOfFrames = imageWidth/frameWidth;

The second argument 10 specifies the base that parseInt() should use.

You can also use the width()(docs) method to get the result without the px.

var frameWidth = 400;
var imageWidth = +$('#inner-image').width();
var numberOfFrames = imageWidth/frameWidth;

Here I used the unary + operator to make it a Number instead of a String.

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

2 Comments

That was it! Thanks for helping me put it together.
+1 for using the base 10 on the parseInt, I have seen so many usages of this function resulting unpredictable parsing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.