I need to store the contents of a HTML table in an array in Javascript. To do so, I would like to know the content type (text/dropdown/input box) of each cell before accessing the content of the cell, so that I can access the cell value accordingly. Kindly guide me how to get to know the content type beforehand.
Asked
Active
Viewed 112 times
0
-
did you look at the cells javascript object? http://www.w3schools.com/jsref/coll_table_cells.asp – Enjoyted Jun 03 '14 at 09:17
-
What about the first answer here? http://stackoverflow.com/questions/8388470/get-element-type-with-jquery – Martin D Jun 03 '14 at 09:24
-
2@Enjoyted [Please don't use w3schools as a resource!!](http://w3fools.com) – nietonfir Jun 03 '14 at 09:36
-
@nietonfir interesting, thanks. – Enjoyted Jun 03 '14 at 09:47
-
What do you mean by “content type of a cell”? HTML has no such concept. You can inspect, with the usual DOM tools, what the content is, but it has no “type”. If you know that the content is always a control (form field) element only, you can inspect its `type` property. – Jukka K. Korpela Jun 03 '14 at 11:35
2 Answers
0
You could use
<table>
<tr>
<td><input type="text" id="something"></td>
<td><select id="something_else"></select></td>
</tr>
</table>
document.getElementById("something").tagName;
//returns: input
document.getElementById("something_else").tagName;
//returns: select

Andy Gee
- 3,149
- 2
- 29
- 44
0
Try something like this
function getValue(cell) { //cell is a ref to table.cell
var child = cell.firstChild;
if(cell.childNodes.length > 1) {//how do you define value for multiple?
console.log('Multiple children not supported');
return;
}
if(child.nodeType === 3) { //text node
return child.textContent;
}
if(child.nodeType === 1) { //elements
switch (child.tagName) { //implement behaviour based on node's tagName
case 'INPUT': return child.value;
case 'SELECT': return child.options[child.selectedIndex].value;
default: return child.innerHTML; //?
}
}
}

Yury Tarabanko
- 44,270
- 9
- 84
- 98