0

I'm trying to change the css filter (hue-rotate to be exact) on a png by using a slider input. I keep getting "-webkit-filter:hue-rotate(undefineddeg)"

Thanks for your help!

Here's my code:

function hueFunction(hueVal) {
  var setAs = hueVal + "deg"
  document.getElementById("lgImage").setAttribute("style", "-webkit-filter:hue-rotate(" + setAs + ")")
}
<input type="range" data-default="0" value="0" min="0" max="360" step="1" id="hue-rotate" oninput="hueFunction(this.hueVal)">

<br/>

<img src="https://campstoregear.com/wp-content/uploads/2017/09/summer-camp-2018-apparel-design-CD1816-Primary.png" id="lgImage">

1 Answer 1

2

Change oninput="hueFunction(this.hueVal)" to oninput="hueFunction(this.value)"

function hueFunction(hueVal) {
  var setAs = hueVal + "deg"
  document.getElementById("lgImage").setAttribute("style", "-webkit-filter:hue-rotate(" + setAs + ")")
}
<input type="range" data-default="0" value="0" min="0" max="360" step="1" id="hue-rotate" oninput="hueFunction(this.value)">

<br/>

<img src="https://campstoregear.com/wp-content/uploads/2017/09/summer-camp-2018-apparel-design-CD1816-Primary.png" id="lgImage">

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

5 Comments

Do you know why this doesn't work with Classes instead of IDs? If I swap "getElementByID" for "getElementsByClassName" it dosn't work.
getElementById returns a single element, getElementsByClassName - an array of elements. You need to specify which one you want to use, like getElementsByClassName('youClassName')[0] jsbin.com/piworezoco/edit?html,js,output
how would I apply this to all elements with that class?
with a for loop, for exampe.. jsbin.com/fopibigabu/edit?js,output
Perfect. Thank you!

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.