For the past few days, I've been coding a tool for some theme customization and needed a simple way to allow users to select their colors.
It seems that you can build an HTML natural color selector by using the color input.
<input type="color" />
You can set it to default stating color by using the value attribute. And you can also use the same attribute to read the selected value in that input.
<input type="color" value="#ff0000" id="color-input" />β©<script type="application/javascript">β© const selectedColor = document.getElementById("color-main").value;β© console.log(document.getElementById("color-main").value);β©</script>
The color will be returned in a hex format (eq: #f00211), the default being #000000.
From a Javascript perspective, we can use the input and change events to track the first selection and any changes in the color input.
colorPicker.addEventListener("input", updateFirst, false);β©colorPicker.addEventListener("change", watchColorPicker, false);
Below is a small working codepen for it:
See the Pen
by JS Craft (@js-craft)
on CodePen.
You can read more about the color input in this article on developer.mozilla.org.
π Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js
π Build a full trivia game app with LangChain
Learn by doing with this FREE ebook! This 35-page guide walks you through every step of building your first fully functional AI-powered app using JavaScript and LangChain.js