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
1 Answer
Reset to default 5When 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
版权声明:本文标题:html - Changing percentage element of css through javascript, using a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745521664a2661652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论