I was struggling to change the value of
-webkit-filter: grayscale(30%);
value dynamically using HTML5 range input and javascript.
How could i access the css webkit filter attribute using javascript ?
I was struggling to change the value of
-webkit-filter: grayscale(30%);
value dynamically using HTML5 range input and javascript.
How could i access the css webkit filter attribute using javascript ?
Share Improve this question asked Sep 6, 2012 at 9:48 Manoj kumarManoj kumar 1342 gold badges3 silver badges10 bronze badges3 Answers
Reset to default 4Javascript:
function changegrayscale(value) {
document.getElementById("divID").setAttribute("style","-webkit-filter:grayscale(" + value + "%)")
}
HTML:
<div id="divID" onclick="changegrayscale('30')">Clcik Me</div>
CSS:
#divID {
-webkit-filter: grayscale(70%);
background: red;
width: 100px;
height: 100px;
}
DEMO
Try this - DEMO
<input type="range" min="0" max="100" step="10" id="inp" />
<img src="http://lorempixel./300/200" id="img" />
<img src="http://lorempixel./300/200" />
<script>
document.getElementById("inp").addEventListener("change", function() {
document.getElementById("img").setAttribute("style", "-webkit-filter:grayscale(" + this.value + "%)");
}, false);
</script>
With jQuery you could do it like this on the '.elements':
function changegrayscale(value) {
$('.elements').css({
'-webkit-filter': 'grayscale('+value+'%)'
});
}