reduce code repetition, put it in a common js library (emoncms.js).
Ensure that document is loaded before adding js events to the DOM (table is threated appart).