0

I have a color input like this

function setstyle(elm, val) {
  elm.style.color = val;
}
<input type="color" name="fontcolor" id="fontcolor" value="#ffffff" onchange="setstyle('usermsg',this.value);">

when you choose a color this will automatically change the style in a separate text area. However, despite the input is in hex, it will always output it as RGB which I don't need. How can I fix that so it adds the equivalent hex value?

fiddle: https://jsfiddle.net/1a23t6w0/

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • https://stackoverflow.com/questions/39208559/browsers-automatically-evaluate-hex-or-hsl-colors-to-rgb-when-setting-via-elemen – Andy Jan 26 '22 at 18:41
  • you mean its impossible? – Cain Nuke Jan 26 '22 at 18:47
  • 1
    Does this answer your question? [Convert rgb strings to hex in Javascript](https://stackoverflow.com/questions/13070054/convert-rgb-strings-to-hex-in-javascript) – Heretic Monkey Jan 26 '22 at 18:47
  • You can convert it to hex as in HM's link, and use that, but the browser will always style it as RGB. – Andy Jan 26 '22 at 18:49
  • 1
    [This answer](https://stackoverflow.com/a/59513865/215552) to the question @Andy posted, and the answers to the question I posted, show how to convert from RGB to hex format, if it's important. That said, the computed value will be in whatever format the browser wants it to be. – Heretic Monkey Jan 26 '22 at 18:50
  • This is really weird. On dev tools if I do this fontcolor.value it outputs the value in hex on the console. So why will it display it otherwise for everything else? – Cain Nuke Jan 26 '22 at 18:52
  • ...Did you *read* the answers to the question posted in the very first comment? – Heretic Monkey Jan 26 '22 at 19:00
  • yes, but still dont get it. – Cain Nuke Jan 26 '22 at 19:01
  • You're not going to be able to get hex color values out of a color input. Since you're already using javascript, also use it to convert the color to the format you desire. – Ouroborus Jan 26 '22 at 20:15

0 Answers0