Check if HTML table has Data in First Row?

Hi,

Is there a way to check if an HTML table has data in the first row in the <td> cells?  Here is my table. I'm guessing javascript can easily do this I just don't know the syntax.  Any help appreciated.  If the row doesn't contain any data then the function should return "false"

<table id="dynamicTable" width="300">
<thead>
      <tr>
      <th>Column1</th>
      <th>Column2</th>
      <th>Column3</th>
      </tr>
</thead>
<tbody>
<tr>
      <td></td>
      <td></td>
      <td></td>
</tr>

</tbody>                        

</table>
<table id="dynamicTable" width="300">
<thead>
	<tr>
	<th>Column1</th>
	<th>Column2</th>
	<th>Column3</th>
	</tr>
</thead>
<tbody>
<tr>
	<td></td>
	<td></td>
	<td></td>
</tr>

</tbody>				

</table>

Open in new window

LVL 1
Westside2004Asked:
Who is Participating?
 
leakim971PluritechnicianCommented:
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
	function checkCellsOfRow1() {
		var empty = true;
		// We use ("tr")[1] and not ("tr")[0] because first row is header of the table
		var cells = document.getElementById("dynamicTable").getElementsByTagName("tr")[1].getElementsByTagName("td");
		for(var c=0;c<cells.length;c++) {
			empty = empty && ( cells[c].innerHTML.length == 0 );
		}
		if( empty ) {
			alert("cells of first row empty !");
		}
		else {
			alert("cells of first row not empty !");
		}
	}
</script>
</head>
<body onload="checkCellsOfRow1();">
<table id="dynamicTable" width="300">
<thead>
	<tr>
	<th>Column1</th>
	<th>Column2</th>
	<th>Column3</th>
	</tr>
</thead>
<tbody>
<tr>
	<td></td>
	<td></td>
	<td></td>
</tr>
</tbody>				
</table>
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
Check this :


<script language="javascript">
	function checkCellsOfRow1() {
		var empty = true;
		// We use ("tr")[1] and not ("tr")[0] because first row is header of the table
		var cells = document.getElementById("dynamicTable").getElementsByTagName("tr")[1].getElementsByTagName("td");
		for(var c=0;c<cells.length;c++) {
			empty = empty && ( cells[c].innerHTML.length == 0 );
		}
		if( empty ) {
			alert("cells of first row empty !");
		}
		else {
			alert("cells of first row not empty !");
		}
	}
</script>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.