Example: Highlight By Age

Highlight Table by Age

function highlightbyage() {
    let tableid = 'senatetable';
    let older = 70;
    let younger = 50;
    let mytable = document.getElementById(tableid);
    console.log("mytable is:");
    console.log(mytable);
    // get tbody (i.e. we dont' want to stripe the thead)
    let mytbody = mytable.getElementsByTagName('tbody');
    console.log("mytbody is:");
    console.log(mytbody);
    /*   getElementsByTagName gives us a Node List, so we need
       to access it as a list (Array), arrayname[i].
       Find all the tr within the tbody
    */
    let myrows =  mytbody[0].getElementsByTagName('tr');
    console.log("myrows is:");
    console.log(myrows);
    console.log("myrows length is: " + myrows.length);

    /*  Iterate over the node list of "tr" returned, setting
        the class appropriately
      */
    for(var i=0; i<myrows.length; i++) {
      let bdaystr = myrows[i].getElementsByTagName('td')[3].firstChild.nodeValue;
      console.log("bdaystr is " + bdaystr);
      let [month, day, year] = bdaystr.split('/');
      console.log(month + " " + day + " " + year);
      let bday = new Date(year, month, day);
      console.log("bday as Date object:");
      console.log(bday)
      let now = Date.now();
      console.log("now is " + now);
      let age = (now - bday)/(1000 * 365.25 * 24 * 60 * 60);
      age = Math.round(age);
      console.log(age);
      if (age >= older) {
        myrows[i].setAttribute('class','older');
      } else if (age <= younger) {
        myrows[i].setAttribute('class','younger');
      }
    }
  }