Dynamically changing Webkit filter values

Problem

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 ?

Problem courtesy of: Manoj kumar

Solution

Javascript:

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

Solution courtesy of: A.K

Discussion

With jQuery you could do it like this on the '.elements':

function changegrayscale(value) {
  $('.elements').css({
    '-webkit-filter': 'grayscale('+value+'%)'
  });
}
Discussion courtesy of: Felix Gertz

Try this - DEMO

<input type="range" min="0" max="100" step="10" id="inp" />

<img src="http://lorempixel.com/300/200" id="img" />
<img src="http://lorempixel.com/300/200" />​

<script>
document.getElementById("inp").addEventListener("change", function() {
    document.getElementById("img").setAttribute("style", "-webkit-filter:grayscale(" + this.value + "%)");
}, false);​
</script>
Discussion courtesy of: Zoltan Toth

This recipe can be found in it's original form on Stack Over Flow.