开发者

Row Index -1 result in Safari and Chrome

Why I get Row Index -1 result in Safari and Chrome when I try to access a dynamic created table row?

The row is appended with document.getElementById('tabl').appendChild(rowobject) and then gets the value of -1 to 开发者_运维问答row Index. Can I fix this somehow?


When referencing the active/selected row in a table, an index of -1 means there is currently no active/selected row. When searching for the row index of a table by the content of some field, a search result of -1 means no such row was found.

This is because tables are 0 index based - meaning the first row is row 0, the second row is row 1, etc. -1 refers explicitly to no row / an invalid row.


When you are creating the table, instead of:

document.getElementById('tabl').appendChild(rowobject)

write:

rowobject = document.getElementById('tabl').insertRow(-1);

This will append a new row to the end of the table, but the new row will now have a correct index even in Chrome and Safari. You can then use returned reference to the rowobject if you want to set some properties to it, such as ID or to append further children such as td.

Have a great day :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜