title: Javascript creating an onclick event for a dynamic table row
body: <p>I'm having trouble attaching an onclick event to my dynamic table rows.</p>
  
  <p>What I have is here:</p>
  
  <pre><code>function parseOrderLine(data) {
      var obj = JSON.parse(data);
      if (obj.length &gt; 0) {
          var tbl = document.createElement('table');
          var row = tbl.insertRow(0);
          var cell = row.insertCell(0);
          cell.innerHTML = "Code";
          cell = row.insertCell(1);
          cell.innerHTML = "Size";
          cell = row.insertCell(2);
          cell.innerHTML = "Description";
          cell = row.insertCell(3);
          cell.innerHTML = "Type";
          cell = row.insertCell(4);
          cell.innerHTML = "Price";
          cell.style.textAlign = "right";
          for (var i = 0; i &lt; obj.length; i++) {
              row = tbl.insertRow(i + 1);
              row.className = "results_row";
              cell = row.insertCell(0);
              cell.innerHTML = obj[i].Code;
              cell = row.insertCell(1);
              cell.innerHTML = obj[i].Size;
              cell = row.insertCell(2);
              cell.innerHTML = obj[i].Description;
              cell = row.insertCell(3);
              cell.innerHTML = obj[i].Type;
              cell = row.insertCell(4);
              cell.innerHTML = parseFloat(obj[i].Price).toFixed(2);
              cell.style.textAlign = "right";
              //row.addEventListener("click", function(){ alert('listen'); });
              row.onclick = (function(){ alert('click'); });
          }
          return tbl.outerHTML;
      }
      else { ...
  </code></pre>
  
  <p>As you can see I have tried both setting the onclick event and adding a listener. Neither seem to work.</p>
  
  <p>I'm hoping someone can tell me I'm just being crazy and have overlooked something obvious.  ;0)</p>
  
  <p>Any help appreciated guys.</p>
  
  <p>Thanks for reading.</p>
  
  <p>Jas</p>