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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.