0

I am running the following, I'd like to have:

spaceInfo, info

      for (y = 0; y < myArrayElement[i].Arg.Space.length; y++) {
        var spaceInfo = document.createElement("p");
        spaceInfo.innerHTML = myArrayElement[i].Arg.Space[y];
        jumbo.appendChild(spaceInfo);
      }

      for (g = 0; g < myArrayElement[i].Arg.Time.length; g++) {
        var info = document.createElement("p");
        info.innerHTML = myArrayElement[i].Arg.Time[g];
        jumbo.appendChild(info);
      }

I tried:

jumbo.appendChild(spaceInfo + ", ");
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • `But that's jQuery not javaScript.` xD – lukaleli Aug 23 '17 at 20:49
  • @jimmyweb ok ok, removed that line.. yet the question remains – rob.m Aug 23 '17 at 20:49
  • that was funny. anyway I don't understand the problem. – lukaleli Aug 23 '17 at 20:50
  • check this out: [How to add comma between array items?](https://stackoverflow.com/a/45546859/6567275) – Thomas Aug 23 '17 at 20:51
  • i simply need to add a comma in `jumbo.appendChild(spaceInfo);` in order to have the output of spaceInfo with a comma – rob.m Aug 23 '17 at 20:51
  • You're appending DOM elements to another dom element, this has nothing to do with strings. If you're trying to visually display a comma in between these two elements, try adding a new div that just has a "," as the inner HTML. – Dr_Derp Aug 23 '17 at 20:52
  • @Dr_Derp that's correct, I did that as I wasn't able to add a comma, yet it's obviously not what I want as it creates 2

    – rob.m Aug 23 '17 at 20:53
  • @Thomas that's actually nice, with css only – rob.m Aug 23 '17 at 20:53
  • Where do you want the comma? At the top you show it between `spaceInfo` and `info`, not between each `spaceInfo` element. – Barmar Aug 23 '17 at 21:00
  • ideally in between the two arrays, the above code is wrong as it is appending two

    – rob.m Aug 23 '17 at 21:01

1 Answers1

0

That MAY work, however I don't fully understand your code. It's out of context.

  for (y = 0; y < myArrayElement[i].Arg.Space.length; y++) {
    var spaceInfo = document.createElement("p");
    spaceInfo.innerHTML = myArrayElement[i].Arg.Space[y] + " ,";
    jumbo.appendChild(spaceInfo);
  }

  for (g = 0; g < myArrayElement[i].Arg.Time.length; g++) {
    var info = document.createElement("p");
    info.innerHTML = myArrayElement[i].Arg.Time[g] + " ,";
    jumbo.appendChild(info);
  }
lukaleli
  • 3,427
  • 3
  • 22
  • 32
  • ok got it, `.Space[y] + " ,";` that's where I should be adding the comma, I was adding it in the append child. Will now change the code not to append on the p. Thanks – rob.m Aug 23 '17 at 20:59
  • 1
    will do as soon as it will let me – rob.m Aug 23 '17 at 21:00
  • bingo, thanks `for (y = 0; y < myArrayElement[i].Arg.Space.length && y < myArrayElement[i].Arg.Time.length; y++) { var spaceInfo = document.createElement("p"); spaceInfo.innerHTML = myArrayElement[i].Arg.Space[y] + ", " + myArrayElement[i].Arg.Time[y]; jumbo.appendChild(spaceInfo); }` – rob.m Aug 23 '17 at 21:10