0

I would like to use a value passed in to a function to define which variables and ids to act upon in the function:

var Song1 = "Do";
var Song2 = "Re";
var Song3 = "Me";

function SendVariable(x){

 alert(Song+x);
 document.getElementById("Song"+x+"Text").innerHTML = Song+x;
 }
  • You'd want to use an array in this case. `const songs = ["Do", "Re", "Mi"];` and then youc an reference a particular one with `songs[x]` – CollinD Jul 22 '22 at 00:36
  • Yeah, i was considering reworking in that direction, but can I do this with Ids? – flog256 Jul 22 '22 at 00:44
  • Sure, if you read the duplicate post linked above, there's mention of how to use objects in a similar way. `const songs = { someId: "Do", another: "Re", aThirdId: "Mi"}` and then it's accessed via `songs[x]` where `x` is a string (ie one of `"someId", "another", "aThirdId") – CollinD Jul 22 '22 at 01:00
  • You da man........................... – flog256 Jul 22 '22 at 02:44

0 Answers0