JavaScript JavaScript and the DOM Getting a Handle on the DOM Select a Page Element By Its ID

Jessica Moszkowicz
Jessica Moszkowicz
3,112 Points

Check for legal input

How would I add a check to app.js that would alert the user if they enter something in myTextInput.value that isn't a color? I tried adding a function that would return the inputted color if it is a color and null if it isn't, but it isn't working as expected. Here is my event listener and the checkColor function:

myButton.addEventListener('click', () => { myHeading.style.color = checkColor(myTextInput.value) });

function checkColor(strColor){ var s = new Option().style; s.color = strColor.toLowerCase; if (s.color == strColor) { return strColor; } else { alert("Please input a color."); return null; } }

1 Answer

Steven Parker
Steven Parker
203,722 Points

When you invoke a function (like "toLowerCase"), you need to add parentheses after the name even when no arguments are being passed.

And don't forget to convert case for the comparison also, or you may get a "false negative".