admin管理员组文章数量:1405157
I try change width of bootstrap progress bar with java script but don't worked.I wrote this code in head section. Please advice.
document.getElementsByClassName("progress-bar").setAttribute("style", "width:50%");
document.getElementsByClassName("progress-bar").style.width = "width:50%";
Html :
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
I try change width of bootstrap progress bar with java script but don't worked.I wrote this code in head section. Please advice.
document.getElementsByClassName("progress-bar").setAttribute("style", "width:50%");
document.getElementsByClassName("progress-bar").style.width = "width:50%";
Html :
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
Share
Improve this question
edited Feb 22, 2016 at 11:17
Zakaria Acharki
67.5k15 gold badges78 silver badges106 bronze badges
asked Feb 22, 2016 at 11:15
hmahdavihmahdavi
2,3824 gold badges46 silver badges98 bronze badges
3
-
document.getElementsByClassName("progress-bar")[0].style.width = "50%";
OR[].forEach.call(document.getElementsByClassName("progress-bar"),function(elem){ elem.style.width="50%" });
– Rayon Commented Feb 22, 2016 at 11:15 - I use this java script code into onload of body tag. I test this way but don't worked – hmahdavi Commented Feb 22, 2016 at 11:18
-
How many elements are there having class as
progress-bar
? – Rayon Commented Feb 22, 2016 at 11:18
2 Answers
Reset to default 3getElementsByClassName() will return a list of nodes, you should specify the index of the one you want to change :
document.getElementsByClassName("progress-bar")[0].setAttribute("style", "width:50%");
document.getElementsByClassName("progress-bar")[0].style.width = "50%";
Hope this helps.
I would suggest you to use document.querySelector
- it returns single element
All major browsers support it
And this is incorrect
document.getElementsByClassName("progress-bar").style.width = "width:50%";
//must be
document.getElementsByClassName("progress-bar").style.width = "50%";
checkout demo below
var percent = 10;
document.querySelector(".progress-bar").style.width = percent + "%";
function increase(){
percent = percent > 90 ? 10 : percent + 10;
document.querySelector(".progress-bar").style.width = percent + "%";
}
.progress{
background: red;
padding: 3px;
}
.progress-bar{
background: blue;
height: 10px;
}
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
<hr />
<button onclick="increase()">increase</button>
本文标签: htmlChange width of progress bar with javascriptStack Overflow
版权声明:本文标题:html - Change width of progress bar with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744272927a2598271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论