1

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!

11
  • 1
    What's exactly the error you're getting? Commented Aug 22, 2014 at 20:52
  • I don't have an error message but I can see that the table refuses to load . Commented Aug 22, 2014 at 20:53
  • 1
    But do you have any error messages in the browser console? Commented Aug 22, 2014 at 20:53
  • Where did you declare the variable html? Commented Aug 22, 2014 at 20:55
  • Tip: You have an unclosed <center> tag in the first block. It's being opened twice. Commented Aug 22, 2014 at 20:57

1 Answer 1

5

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>";
Sign up to request clarification or add additional context in comments.

7 Comments

taken from @undefined in the comments below the question
Good spot, but I don't think this would stop the table to be rendered.
Huh? What's @undefined?
Well, every programmer knows how to concatenate the strings :)
If the table wasn't loading when you tried to take the array reference out of the quotes before swapping in my version of the line, I suspect it might have been just a misplaced or missing quote-- it's one of the reasons why building HTML with strings can be tricky.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.