Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top