Solved

# Table value math

Posted on 2009-04-17
1,158 Views
I'm new to JavaScript and jQuery, and I'm currently converting some JavaScript code to jQuery (for learning purposes). I have a table (picture attached), and I am doing some math on the the values in that table. The values that I want to retrieve have been set with an ID with a particular prefix, and the cells that I need to perform math on are all in either the same row or column.

The Quantity column is entered by the user and the rest of the cells are read-only.
The Subtotal column is Price*Quantity
The Discount column is Price*20%
The Total column is Subtotal-Discount
The TOTAL row is just the sum of the corresponding columns.

Each has a specific ID so that its easy to reference them. For example, all cells in the Quantity column would have the id "qty_"+item_name. And cells in the Subtotal column would have the id "subtot_"+itemname.

Now for my question, how do I go about doing the math? Is there a simpler way to do the addition and subtraction than by getting all the elements by ID and going through a for loop? Can I grab just the position of the cells and then go from there?

table.JPG
0
Question by:Corinne_09

LVL 36

Expert Comment

This is untested, and I'm a little rusty, but I believe you can access the cells of an ordinary <table> in the following way:

var tbl = document.getElementById('TABLE_ID');

or something like that to get the top left cell of the table. Is this what you are asking about?
0

Author Comment

No, I was asking how to do it using jQuery. I do have it currently in JavaScript, and I'm converting it to jQuery. but it's a little confusing.
0

LVL 36

Accepted Solution

There is the :eq selector: http://docs.jquery.com/Selectors/eq#index which you might find useful.
0

Author Closing Comment

It wasn't the exact solution to my answer, but that helped a lot. It gave me a clue as to how to retrieve the table cell value and use the information to do calculations. Thanks!
0

## Featured Post

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…