Question

In JavaScript, are rows and cells dynamic collections?

For example:

var myRows=myTable.rows;
var newRow=myTable.insertRow();

Will newRow automatically become part of myRows?

Sorry if it seems like a basic question, but I couldn't find any reference with a clear answer.

Was it helpful?

Solution

table.rows is* an HTMLCollection which is live.

An HTMLCollection is a list of nodes. An individual node may be accessed by either ordinal index or the node's name or id attributes.

Note: Collections in the HTML DOM are assumed to be live meaning that they are automatically updated when the underlying document is changed.


* As specified here:

Object HTMLTableElement

...

rows

This property is of type HTMLCollection.

OTHER TIPS

Just try it and see:

http://jsfiddle.net/xfecU/

<table>
    <tr><td></td></tr>
</table>
<script>
    var myTable = document.getElementsByTagName('table')[0];
    var myRows=myTable.rows;
    alert(myRows.length); //alerts 1
    var newRow=myTable.insertRow();
    alert(myRows.length); //alerts 2
</script>​

So yes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top