Below is the problematic code in question. It works except for polygon.setAttribute("onclick", "logMe(this)"); line which throws the error
polygon.setAttribute("onclick", "logMe(this)");