create color picker in javascript

To create a color picker in JavaScript, you can use input type="color" in HTML. Here's an example:

index.tsx
<input type="color" id="colorPicker">
38 chars
2 lines

You can then access the value of the color picked using JavaScript by using the value property of the color picker element:

index.tsx
const colorPicker = document.getElementById('colorPicker');
const selectedColor = colorPicker.value;
console.log(selectedColor); // will output a hex code e.g. #00FF00 for green
178 chars
4 lines

To handle changes to the color picker value, you can add an event listener:

index.tsx
colorPicker.addEventListener('change', function() {
  const selectedColor = colorPicker.value;
  console.log(selectedColor); // will output a hex code e.g. #FF0000 for red
  // do something with selectedColor value here
});
224 chars
6 lines

In terms of styling the color picker, you can use CSS to modify the appearance of the color picker element and label:

label {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-weight: bold;
}

input[type="color"] {
  border: none;
  width: 50px;
  height: 50px;
}
153 chars
12 lines

And in your HTML, you can wrap the color picker in a label element to add a label:

<label for="colorPicker">Pick a color:</label>
<input type="color" id="colorPicker">
85 chars
3 lines

gistlibby LogSnag