admin管理员组

文章数量:1428327

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.

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.

Share Improve this question edited Nov 13, 2012 at 18:07 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Nov 13, 2012 at 17:58 OliverOliver 1111 gold badge2 silver badges10 bronze badges 2
  • I believe this is the same question as this. Maybe I was wrong with understanding of your question, what have you tried and didn't work? – gdoron Commented Nov 13, 2012 at 18:01
  • Hey, thanks for replying! Your solution would have worked great, but I needed to calculate a value and then append a string like "%" or "px". I don't quite know this ins and outs of javascript, and didn't know that you could convert from int to string just by appending a string. – Oliver Commented Nov 13, 2012 at 18:38
Add a ment  | 

1 Answer 1

Reset to default 5

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

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

本文标签: htmlChanging percentage element of css through javascriptusing a variableStack Overflow