Javascript Element How to - Handle click event for table cell td








Question

We would like to know how to handle click event for table cell td.

Answer


<!--from   w ww .  j  a  va 2  s  . c  o m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function colorChanger(el){
    el.style.backgroundColor = '#007d00';
}

</script>
</head>
<body>
  <table>
    <tr>
      <td onclick="colorChanger(this);">click me</td>
    </tr>
  </table>
</body>
</html>

The code above is rendered as follows: