0

I have multiple outputs in html. I want to reach them with for loop. For example: unitsTotal = 4;

Then I want to have (output1, tag1), (output2, tag2), (output3, tag3), (output4, tag4).

And I don't know how to pass i inside a string. Can someone help me with doing that? Maybe there is a better idea how to make it.

for (var i = 1; i < unitsTotal; i += 1) {
        $(':input[name="output"]').val(output1.toFixed(5).toString());
        $('#output').text(tag1);    
    }
  • 1
    It looks like you use variable variables. *"Maybe there is a better idea how to make it."* Use an array and index into it. [Learn about arrays from MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). Note: `.toFixed` already returns a string, there is no need to call `.toString`. – Felix Kling Apr 28 '15 at 17:00
  • it's not very clear what you're asking here? Are you just looking for `$('#output').text("tag" + i);` – adeneo Apr 28 '15 at 17:01
  • And would that even work with an ID, it's unique, so there's only element with that ID ? – adeneo Apr 28 '15 at 17:01
  • $(':input[name="output1"]') .... $(':input[name="output2"]') .... $(':input[name="output3"]') That is what I want to get – Filip Vojtíšek Apr 28 '15 at 17:03
  • Ah. You just have to use string concatenation. – Felix Kling Apr 28 '15 at 17:03

0 Answers0