Hello, just having a little trouble with the use of the toLowerCase() function. My project is to edit text in a text area. I have a basic change colour function which gets the user to put either red, blue, green or yellow into it to a
textbox. It usessome if statements, which was working fine until I added the toLowerCase() function. Here is my
code.
for JavaScript:
function changeColour() {
var color = document.forms["myForm2"]["myTextBox"].value;
colour = color.toLowerCase();
if(colour == "red") {
document.myForm1.myTextArea.style.color ="red";
} else if (colour == "blue") {
document.myForm1.myTextArea.style.color ="blue";
} else if (colour == "green") {
document.myForm1.myTextArea.style.color ="green";
} else if (colour == "yellow") {
document.myForm1.myTextArea.style.color ="yellow";
} else { alert("Please enter red, blue green or yellow")
}
That was an interesting solution.
Could you expand on what this part:
Code:
function changeColour( theForm ) {
var color = {
red : "red",
blue : "blue",
green : "green",
yellow : "yellow"
}[ theForm["myTextBox"].value.toLowerCase() ];
is doing. I understand the ternary statement right below it.
How does putting the value within the brackets [ ] get the color values?
I guess I'm unclear on this part of the concept.
I also noticed you could alter the ternary statement to:
Code:
color ? theForm["myTextArea"].style.color = color
: alert("Please enter red, blue green or yellow");
since you already have 'theForm' parameter in the function.
Bookmarks