0

I need to execute the following command $("#title").html( choice# + " wins! "); but instead of "#", it should be an integer and that should trigger variables I have stored called choice1 - choice10 are variables hence when the code is executed, the string stored in choice1 - choice10 (depending on what is chosen) should show on the innerHTML.

So far I tried $("#title").html( "choice" + position + " wins! "); where var position; is a random number and it changes the html to literally "choice3 wins! " (or whatever number it was) instead of printing the string in var choice3;.

  • If it is a global variable, you can try `window['choice'+ position]` for the value of that variable – Taplar Sep 01 '20 at 16:46
  • Instead of doing all that just use an array `var choices = ['choise one', 'choise two', ..]` and then you can use `choices[position]` – Titus Sep 01 '20 at 16:49
  • @Taplar No it's inside an event listener function... –  Sep 01 '20 at 16:53
  • @Titus No because each choice (string) is assigned a value which will be shown on the website and the highest value wins... –  Sep 01 '20 at 16:54
  • That is not a reason not to use an array. – Titus Sep 01 '20 at 17:01
  • Oh I get what you mean thanks!!! Let me try if it would work –  Sep 01 '20 at 17:07

0 Answers0