Reputation: 573
I am trying to create a dynamic bar in HTML using javascript. I have create the button but cannot seem to pass the value over to the progress bar. Can someone please help me? thanks!
<button onclick="increase()">Add</button>
<button onclick="decrease()">Minus</button>
<input type="text" id="tb">
<script type="text/javascript">
var value = 0 document.getElementById("tb").value = value;
function increase(){
this.value = value + 1; document.getElementById("tb").value=value;
}
function decrease(){
this.value = value - 1; document.getElementById("tb").value=value;
}
document.write("<div class='meter'><span style='width: 30%'></span> </div>");
document.write("<input type='text' id=\"tb\">"+value +" </input>");
</script>
Upvotes: 2
Views: 20732
Reputation: 18022
It'd be easier to do this in jQuery, but here it goes with POJS:
js:
var value = 0,
tb = document.getElementById("tb"),
progress = document.getElementById("progress"); //store these, it's better
function increase(){
value++;// same as value += 1, but better
if(value>=100) value = 100;//keep it under 100%
tb.value = value;// set the value of the text field
progress.style.width = value + "%";// set the width of the progress bar
}
function decrease(){
value--;
if(value<=0) value = 0;//keep it over 0%
tb.value = value;
progress.style.width = value + "%";
}
document.write
is janky, so I ditched that & put the bar in the markup.
html:
<button onclick="increase()">Add</button>
<button onclick="decrease()">Minus</button>
<input type="text" id="tb">
<div id='meter'><div id='progress'></div></div>
css:
#meter {border:1px solid #000;width:100px}
#progress {background:#333;height:10px;width:0%}
fiddle: http://jsfiddle.net/sw95b/
Upvotes: 3