Reputation: 175
I used this above html code for my project..but i don't know how to get value of above input using javascript
<form>
<input type="color" id="favcolor">
</form>
can someone help me ?
Thanks
Upvotes: 12
Views: 40254
Reputation: 411
To simply get the value
document.getElementById("favcolor").value;
You can add an event listener if you want to get the color when the selection changes. You’d do something like this:
var theInput = document.getElementById("favcolor");
theInput.addEventListener("input", function(){
var theColor = theInput.value;
// Do something with `theColor` here.
}, false);
<input id="favcolor" type="color"/>
Here’s a working JSFiddle.
Upvotes: 25
Reputation: 21
We can simply assign the color value to the label.
Sample code is here
<label id ="colorVal">Select color</label>
<input type="color" id ='color'>Value</input>
JS Code
let colorInput = document.getElementById('color');
colorInput.addEventListener('input', () =>{
document.getElementById('colorVal').innerHTML = colorInput.value;
});
Sample codepen link
https://codepen.io/ragi_jay/pen/poRMwMX
Upvotes: 0