var arr = ['a', 'b', 'c'];
for (var prop in arr)
alert (prop + ":"+ arr[prop]);
Result:
0:a
1:b
2:c
This blog features pure JavaScript code. You don't need to use libraries to do stuff in JavaScript.
var tbl; function getParent(el, pTagName) { if (el == null) return null; else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) return el; else return getParent(el.parentNode, pTagName); } function toggleSection(lnk){ var td = lnk.parentNode; var table = getParent(td,'TABLE'); var len = table.rows.length; var tr = getParent(td, 'tr'); var rowIndex = tr.rowIndex; var rowHead=table.rows[rowIndex].cells[1].innerHTML; lnk.innerHTML =(lnk.innerHTML == "+")?"-":"+"; vStyle =(tbl.rows[rowIndex+1].style.display=='none')?'':'none'; for(var i = rowIndex+1; i < len;i++){ if (table.rows[i].cells[1].innerHTML==rowHead){ table.rows[i].style.display= vStyle; table.rows[i].cells[1].style.visibility="hidden"; } } } function collapseRows(){ tables =document.getElementsByTagName("table"); for(i =0; i < tables.length;i++){ if(tables[i].className.indexOf("expandable") != -1) tbl =tables[i]; } if(typeof tbl=='undefined'){ alert("Could not find a table of expandable class"); return; } //assume the first row is headings and the first column is empty var len = tbl.rows.length; var link = '<a href="#" onclick="toggleSection(this);return false;">+ </a> '; var rowHead = tbl.rows[1].cells[1].innerHTML; for (j=1; j < len;j++){ //check the value in each row of column 2 var m = tbl.rows[j].cells[1].innerHTML; if(m!=rowHead || j==1){ rowHead=m; tbl.rows[j].cells[0].innerHTML = link; tbl.rows[j].style.background = "#CCFF99"; } else tbl.rows[j].style.display = "none"; } } window.onload=collapseRows;
var len = tbl.rows.length;var link = '<a href="#" onclick="toggleSection(this);return false;">+ </a> ';
var rowHead = tbl.rows[1].cells[1].innerHTML;for (j=1; j < len;j++){ var m = tbl.rows[j].cells[1].innerHTML; if(m!=rowHead || j==1){ rowHead=m; tbl.rows[j].cells[0].innerHTML = link; tbl.rows[j].style.background = "#CCFF99"; } else tbl.rows[j].style.display = "none"; }The next function, toggleSection, hides or collapses rows with duplicate months. As a parameter, the function accepts a reference to the link that calls the function. Using the link reference, we get a reference to the containing table cell, row, and then the row index for the cell:
var rowHead=table.rows[rowIndex].cells[1].innerHTML;
Next, we switch the text of the link from plus to minus or vice versa.
lnk.innerHTML =(lnk.innerHTML == "+")?"-":"+";
Then, we prepare a style to be used for the next rows.
vStyle =(tbl.rows[rowIndex+1].style.display=='none')?'':'none';
Then, we loop through the table, and if the text in the second column of each row is the same as the text in the same column of the row that contains the link, we hide or show that row depending on its initial state.
for(var i = rowIndex+1; i < len;i++){
if (table.rows[i].cells[1].innerHTML==rowHead){
table.rows[i].style.display= vStyle;
}
We also hide the repeating text in the second column:
table.rows[i].cells[1].style.visibility="hidden";
Don't forget, the table must contain an empty first column to hold a plus or minus sign and have a class named 'expandable'.
See a demo here.
The script can be downloaded here.
Another post dealing with the HTML table :
Highlight Table Cells with JavaScript