?
Solved

Table value math

Posted on 2009-04-17
4
Medium Priority
?
1,161 Views
Last Modified: 2012-05-06
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?

Thanks for your help.
table.JPG
0
Comment
Question by:Corinne_09
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 24170244
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');
alert(tbl.rows[0].cells[0].innerHTML);

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

Author Comment

by:Corinne_09
ID: 24170677
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

by:
Zyloch earned 375 total points
ID: 24171131
There is the :eq selector: http://docs.jquery.com/Selectors/eq#index which you might find useful.
0
 

Author Closing Comment

by:Corinne_09
ID: 31571509
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question