I’ve been working on some Javascript code to perform some math operations on an HTML table. It first calculates an average for a row and then calculates the total of the average for all rows with the same selection from a menu.
The average calculation is working correctly but I’ve broken something with the total of the averages. I’ve setup a sample JSFiddle:
http://jsfiddle.net/fmdataweb/c4gJU/3/
If you select “moderate” as the risk from the menu and enter some numbers for the Hours Per Week and Weeks Per Year it will calculate the average, but it should then put the average into the Total of Moderate cell. Same if you changed the menu selection to “high”.
Appreciate if someone can point out where I’m going wrong – I’m a Javascript newbie at this stage.
The selector
$('#lastYear tr:has(.risk)').eq(i).find('td:last input')is not matching any element.TEST:
If you insert the line
alert($('#lastYear tr:has(.risk)').eq(i).find('td:last input').length)below the linevar qty = parseFloat(...)the pop up window will display0.EDIT:
As @Diego pointed out, your original code is matching a cell having a button, not an
input. The code above selects the cell before the last one and then gets its input. However I’d recommend you using custom classes or id’s to make your selector shorter and easier to read.