Stuck in switch statement

with a little help from Mark Hunte I got a part to work. But now I need it to be possible to change the outcome when a different button is clicked. I tried the switch statement, but obviously I am doing something wrong. So what I want to happen: if you click btn choose1, text color must be red green blue.
When you click btn choose2 text color must be yellow brown white.
Any help (24.4 KB)


I declared the arrays as global variables in a new on scene load js. “init()”

This means it will be available to all javascript on the page (afaik)

You should read MDN Switch about switches and how they work. Yours was completely wrong in it’s setup.

In ours once we switch to the right case, we only need to determine which colours or names we are going to use and set the global array variables accordingly.

When the second set of buttons ( name, color) calls their script; that script will be able to read those global array variables. So we do not need to do anything there with them apart from tell the textfields to use them. (28.6 KB)

Also note.

Javascript indexes (numbering of items for example in an array) start from 0 not 1.


0 = 1
1= 2
3= 4

And so on.

So in the line.

hypeDocument.getElementById("text1").innerHTML = eval([buttonID][0]) [0];

we are saying:

get the element named “text1”, and set it’s innerHTML to: item 1 of some array.

The item number is determined by the very last [0].

The eval([buttonID][0]) is just turning a string into a variable . And we have to use the [0] on the end of “buttonID][0]” or we will get the name of the array instead of to its items. I think this is only because we are using eval(). But someone with better knowledge may be able to clarify but suffice to say I am only pointing out as an anomaly to my explanation of indexes.

Thanks Mark, that's a great explanation. I'm new to using arrays and I appreciate any help I can get.

Thank you very much Mark, I studied your switch statement and I think I know what I did wrong. The window.thing is also very important. I see that now.
But why do you use the ‘console.log()’? It seems it has no use.

The console.log() should have been commented out.

But what I use it for is debugging.

Search google for it to get a fuller explanation.

But we can print out data that is within its ()

If we place a variable in it, the data the variable holds will be displayed in the JavaScript console.

Look up enable developer menu in Safari and using the JavaScript console.