I am trying to add text to each cell in particular column via Javascript. Like every 8th TD would be processed for adding text.
Tell me what I have done wrong here/why it doesn’t appear in my table:
<script type="text/javascript">
window.onload = function inventorytable() {
var tableRows = document.getElementById
("inventorytable").getElementsByTagName("tbody")[0].getElementsByTagName("tr");
for(var i = 0, l = tableRows.length; i < l; i++) {
tds = tableRows.getElementsByTagName("td");
var processor = tds[8].innerHTML += " Ghz"
var ram = tds[9].innerHTML += " GB"
var rspeed = tds[11].innerHTML += " Mhz"
}}
</script>
You’ve forgotten to refer to the i-th row:
I recommend to use:
Do not define unused variables. If you want to clarify their use, use comments.
Also, I have replaced
.getElementsByTagNameby.rowsand.cells.Update
JavaScript sets are zero-based. So, if you want to refer to the 4th cell, use
.cells[3].At your previous answer, you’ve showed a fiddle. see http://jsfiddle.net/ndfh2/.
As you can see, the first row is also getting postfixes. To not add postfixes to these cells in the first row, initiate the counter at one:
for( var i=1; .. ; .. )Your current code does probably not work, because your rows don’t have twelve (12) rows. Remember, the number at
tds[ number ]equals the index of a cell within a row, starting at zero.