We help IT Professionals succeed at work.
Get Started

How can I add the values in a table column with CSS3 and jQuery?

mrcoulson asked
Last Modified: 2013-11-11
Here's a link to my test page: http://www.co.frederick.va.us/dev/jquery_math.html.  It's simpler than where this code will ultimately go.

I want to add the values in the second column.  I found this little tutorial, but I ran into some problems: http://naspinski.net/post/Use-jQuery-to-add-all-the-values-in-a-table-column_.aspx.

It doesn't work.  Firefox debug console tells me "Unknown pseudo-class or pseudo-element 'gt'." What's up?

I'll go ahead and attach my code, but it may change on the page as I tinker with this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"></script>
<table id="testTable">
		<th>Column A</th>
		<th>Column B</th>
<div id="result"></div>
<script type="text/javascript">
	// $("td:nth-child(2)").append("<span> - 2nd!</span>");
	// This stuff came from: http://naspinski.net/post/Use-jQuery-to-add-all-the-values-in-a-table-column_.aspx .
    var total = 0;
	var rows = $("#summaryTable tr:gt(0)");
	rows.children("td:nth-child(2)").each(function() {
    	total += parseFloat($(this).html());

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE