Rrryyyaaannn
Rrryyyaaannn

Reputation: 2597

Javascript/Jquery : Mathematically Divide Two Variables

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!

Upvotes: 7

Views: 15024

Answers (1)

user113716
user113716

Reputation: 322492

.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.

Upvotes: 15

Related Questions