Oliver
Oliver

Reputation: 111

Changing percentage element of css through javascript, using a variable

I am trying to change a the width value of my div using javascript. Using:

document.getElementById("id").style.width = "25%";

works fine for string literals, but I want to set the value using a variable.

Upvotes: 2

Views: 4972

Answers (1)

Rich O'Kelly
Rich O'Kelly

Reputation: 41757

When concatenating strings and variables javascript will use their string representation, try the following:

var variable = 25;
document.getElementById("id").style.width = variable + "%";

Upvotes: 5

Related Questions