I would like to change color of the header based on what color the user chooses and types in in the input box provided.
I just can’t figure out how to type correctly
if (user input === color){
document.getElementById("header").style.color ="that color"
in other words if user input equals a color type, change the header color to the color specified.
I’m not sure if adding ids to the list helps in any way, sorry this is one of my first exercises in JS
This is the HTML:
<h1 id="header"> Hi There </h1>
<p>Choose from one of the following options!</p>
<ul>
<li id="darkred">Dark red</li>
<li id = "darkorange">Dark orange</li>
<li id = "yellow">Yellow</li>
<li id = "darkgreen">Dark Green</li>
<li id = "teal">Teal</li>
<li id = "navy">Navy</li>
<li id = "indigo">Indigo</li>
<li id = "fuchsia">Fuchsia</li>
<li id = "lavender">Lavender</li>
<li id = "pink">Pink</li>
</ul>
<input type-"text" id="inp"/>
I mean i could write functions for every one of those colors and that would work, but that would be also be unnecessarily long and dumb and I’m trying to make more general functions here
3
Answers
You can create a button on click of which you will get the
input color
and then set it.But if you want to restrict the user to select only color from the list then you can get all colors and then match from it.
Ig you want to set background color when user provides an input. If yes then you can try this.
Code Explanation
input – It’s an Event just like click event.
e – It’s the event. You can call whatever u want.
Correct Me If I’m Wrong
Here is an example using click on the LI instead of ppl having to type.
You show the list anyway so why not?
Also some colour combinations with the text colour have poor contrast, so using a class you can tweak the foreground colour