0

I did code a simple css process bar. I need to get range or textbox value to Div width <div style="width:50%;">, When the user change value of range or textbox like textbox takes range value in this code. Can I do it using javascript? *I'm not good with JS so no idea how do I start! Is it possible to do?

here's my simple code:

.graph {
width:200px; margin-left:25px; height:20px;
background: rgb(168,168,168);
background: -moz-linear-gradient(top, rgba(168,168,168,1) 0%, rgba(204,204,204,1) 23%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(168,168,168,1)), color-stop(23%,rgba(204,204,204,1)));
background: -webkit-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -o-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -ms-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a8a8a8', endColorstr='#cccccc',GradientType=0 );
background: linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
position: relative;
  }
#bar {
background:#09C; height:20px;               
}
<div id="progress" class="graph"><div id="bar" style="width:50%;"></div></div>
<br>
<form>
<input type="range" min="0" max="100"  step="5" onchange="rangevalue.value=value" id="textvalue" />
<br>
<input type="text" maxlength="3" id="rangevalue" onchange="textvalue.value=value">
</form>

3 Answers 3

3

Simple, just hook up a change event to the slider & textbox, and adjust the bar appropriately.

$(document).on('change','#rangevalue, #textvalue',function(){
  $('#bar').css('width',$(this).val() + '%');
  });
.graph {
width:200px; margin-left:25px; height:20px;
background: rgb(168,168,168);
background: -moz-linear-gradient(top, rgba(168,168,168,1) 0%, rgba(204,204,204,1) 23%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(168,168,168,1)), color-stop(23%,rgba(204,204,204,1)));
background: -webkit-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -o-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -ms-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a8a8a8', endColorstr='#cccccc',GradientType=0 );
background: linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
position: relative;
  }
#bar {
background:#09C; height:20px;               
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="progress" class="graph"><div id="bar" style="width:50%;"></div></div>
<br>
<form>
<input type="range" min="0" max="100"  step="5" onchange="rangevalue.value=value" id="textvalue" />
<br>
<input type="text" maxlength="3" id="rangevalue" onchange="textvalue.value=value">
</form>

Sign up to request clarification or add additional context in comments.

1 Comment

@VarunaLex Just change this $(document).on('input change', to $(document).on('input change', and you can have realtime change in the CSS Bar. Cheers,
1

Alternatively, you could use the jQueryUI slider library to do something like this:

FIDDLE

$(function () {
    var $slider = $("#slider-range-min").slider({
        range: "min",
        value: $('#amount').val(),
        min: 0,
        max: 100,
        step: 5,
        slide: function (event, ui) {
            $("#amount").val(ui.value);
        }
    });
    $("#amount").keyup(function(e) {
        $slider.slider('value', $(this).val());   
    });
});

HTML

<p>
    <label for="amount">Progress</label>
    <input type="number" id="amount" value="20" style="border:1; color:#f6931f; font-weight:bold;">
</p>
<div id="slider-range-min"></div>

Comments

1

~updated to change the textbox.

function changeGraph(rangeValue){
  document.getElementById('bar').style.width=rangeValue+"%";
  document.getElementById('rangevalue').value=rangeValue;
}
.graph {
width:200px; margin-left:25px; height:20px;
background: rgb(168,168,168);
background: -moz-linear-gradient(top, rgba(168,168,168,1) 0%, rgba(204,204,204,1) 23%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(168,168,168,1)), color-stop(23%,rgba(204,204,204,1)));
background: -webkit-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -o-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
background: -ms-linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a8a8a8', endColorstr='#cccccc',GradientType=0 );
background: linear-gradient(top, rgba(168,168,168,1) 0%,rgba(204,204,204,1) 23%);
position: relative;
  }
#bar {
background:#09C; height:20px;               
}
<div id="progress" class="graph"><div id="bar" style="width:50%;"></div></div>
<br>
<form>
<input type="range" min="0" max="100"  step="5" onchange="changeGraph(this.value)" />
<br>
<input type="text" maxlength="3" id="rangevalue" >
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.