maxisme
maxisme

Reputation: 4245

.width(variable) as a percentage instead of pixels

I have a variable perc which is a number that changes from 0 to 100. When I use jquery to adjust the width of a progress bar it just does it in pixels.

var perc = ((first/constant)*100).toFixed(0);

.width(perc)

How can I do it as a percentage?

Upvotes: 3

Views: 2804

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

Use .css() instead

.css('width', perc + '%')

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195952

add the % symbol after the number

.width(perc+'%')

If you read the documentation of the .width(value) method you will see that it accepts both numbers and strings..

If a string is provided, however, any valid CSS measurement may be used for the width (such as 100px, 50%, or auto).

Upvotes: 5

Related Questions