MadsterMaddness
MadsterMaddness

Reputation: 735

HTML Javascript Adding a var as an input value inside a for loop

What I am doing is creating a table. And I am adding the values of this table using Javascript with html statements inside of a for loop. I know this may be hard to visualize so I have added a picture to show the radio buttons to the left hand side with the corresponding data on the rest of the row. I am trying to give the radio button the same value as my site id.

Here is an example of my table.

table ex

Here is my for loop:

   //html is the html code that will be converted from javascript to html, and the array for siteID is also declared before the for loop   
  

 //for loop to add the results to the array and also to the table
    for (var i = 0; i < results4.length; i++) {
      var object4 = results4[i];
      //the value of the site ID
      siteIDArray[i] = object4.get('SiteID');
     
      html += "<tr><td><center><br><input type='radio' onclick='alert(this.value)' 
                name='siteChosen' value='siteIDArray[i]'><center></td>";
      html += "<td><center><p>" + siteIDArray[i] + "</p></center></td>";
      ...  </tr>

 }
     

My problem is this just alerts the text siteIDArray[i], and when I tried to do value='"+siteIDArray[i]+"' and value="+siteIDArray[i]+" then the table doesn't load. I feel like I am really close. Am I just missing a small thing?


SOLVED using Elliot de Vries's answer below!

Upvotes: 1

Views: 836

Answers (1)

user458962
user458962

Reputation:

Right now the value of the "value" attribute is the string "siteIDArray[i]" and not its value. You will want to take it outside of the quotes:

html += "<tr><td><center><br><input type='radio' onclick='alert(this.value)' 
                name='siteChosen' value='" + siteIDArray[i] + "'><center></td>";

Upvotes: 5

Related Questions