0

Please refer to the variable "TEST", how to that it become innerHTML and not like plain text when onload?

<!DOCTYPE html>
<head>
<title>Sum Table Column</title>
<script>

window.onload=function() {

var TEST = "<font color='#ff0000'>Apple</font>";

  var values = new Array(3);
  values[0] = [123.45, TEST, true] ;
  values[1] = [65, "banana", false];
  values[2] = [1034.99, "cherry", false];

  var mixed = document.getElementById("mixed");

  // IE7 only supports appending rows to tbody
  var tbody = document.createElement("tbody");

  // for each outer array row
  for (var i = 0 ; i < values.length; i++) {
     var tr = document.createElement("tr");

     // for each inner array cell
     // create td then text, append
     for (var j = 0; j < values[i].length; j++) {
       var td = document.createElement("td");
       var txt = document.createTextNode(values[i][j]);
       td.appendChild(txt);
       tr.appendChild(td);
     }

     // append row to table
     // IE7 requires append row to tbody, append tbody to table
     tbody.appendChild(tr);
     mixed.appendChild(tbody);
   }

}

</script>

</head>
<body>
<table id="mixed">
<tr><th>Value One</th><th>Value two</th><th>Value three</th></tr>
</table>
</body>

Please refer to the variable "TEST", how to that it become innerHTML and not like plain text when onload?

1
  • BTW, you can avoid messing with the tbody using row = table.insertRow(), which appends the row as the last row in the table and assigns a reference to row. See MDN insertRow. Also, it would be better to implement highlighting by applying a class so that you just mess with the CSS to change how the text looks, rather than having to mess with javascript when you want it, say, green instead of red. The font element was deprecated in HTML 4 and is removed in HTML 5. Commented Sep 13, 2015 at 23:58

1 Answer 1

3

Try substituting td.innerHTML = values[i][j]; for var txt = document.createTextNode(values[i][j]);

<!DOCTYPE html>
<head>
<title>Sum Table Column</title>
<script>

window.onload=function() {

var TEST = "<font color='#ff0000'>Apple</font>";

  var values = new Array(3);
  values[0] = [123.45, TEST, true] ;
  values[1] = [65, "banana", false];
  values[2] = [1034.99, "cherry", false];

  var mixed = document.getElementById("mixed");

  // IE7 only supports appending rows to tbody
  var tbody = document.createElement("tbody");

  // for each outer array row
  for (var i = 0 ; i < values.length; i++) {
     var tr = document.createElement("tr");

     // for each inner array cell
     // create td then text, append
     for (var j = 0; j < values[i].length; j++) {
       var td = document.createElement("td");
       td.innerHTML = values[i][j];
       tr.appendChild(td);
     }

     // append row to table
     // IE7 requires append row to tbody, append tbody to table
     tbody.appendChild(tr);
     mixed.appendChild(tbody);
   }

}

</script>

</head>
<body>
<table id="mixed">
<tr><th>Value One</th><th>Value two</th><th>Value three</th></tr>
</table>
</body>

Sign up to request clarification or add additional context in comments.

1 Comment

I have been looking for ways and ran out of time but thank you very much. You responded quickly and give the correct solution. @guest271314

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.