Thursday, March 10, 2011

Onclick event on Table, tr and td

Hello Friends,

I share the knowledge about onclick event for table, tr and td.

for e.g.
<table>
       <tr>
             <td id="td1" onclick="clickMe()">Hello</td>
             <td id="td2" onclick="clickMe(id)">How r u?</td>
         </tr>
</table>
<script>
      function clickMe(tdId){
                     alert(document.getElementById(tdId).id);
                     alert(document.getElementById(tdId).innerHTML);
        }
</script>

same way you can set click event on tr and table and call function.
Also you can write inline function in onclick event so no need to write explict function for the event.

for e.g.
<td id='td1' onclick="alert(document.getElementById('td1').innerHTML)" > Hello Friends </td>