Table value math

Posted on 2009-04-17
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.
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?

    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.
    LVL 36

    Accepted Solution

    There is the :eq selector: which you might find useful.

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    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…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now