There is a div with three slide-bars, each representing an 'rgb' value with a range of 0-255. When the slide-bars are dragged, the background-color of the div should change accordingly. Currently, an 'onchange' event is called to a function with two parameters (this.value and a number) to set the background-color of the div. However, the color is only changing when the 'onmouseup' action occurs, instead of dynamically as the slide-bar is being dragged.
Here is the code:
<html>
<head>
<script>
var r=0;
var g=0;
var b=0;
function set(){
r=g=b=128;
document.getElementById('div').style.backgroundColor="rgb("+r+","+g+","+b+")";
}
function change(val,bar){
if (bar==1){
r=val;
}
if (bar==2){
g=val;
}
if (bar==3){
b=val;
}
document.getElementById('div').style.backgroundColor="rgb("+r+","+g+","+b+")";
}
</script>
</head>
<body onload="set()">
<div id="div" style="width:400px;height:100px;">
</div>
<table border="">
<tr><td>red</td><td><input id="r_slide" type="range" style="width:200px" min="0" max="255" onchange="change(this.value,1)"></td></tr>
<tr><td>blue</td><td><input id="g_slide" type="range" style="width:200px" min="0" max="255" onchange="change(this.value,2)"></td></tr>
<tr><td>green</td><td><input id="b_slide" type="range" style="width:200px" min="0" max="255" onchange="change(this.value,3)"></td></tr>
</table>
</body>
</html>