How do I change HTML table cell color on click

28,939

Ugly, but demonstrates the effect:

  <table>
    <tr>
        <td onclick="this.style.backgroundColor = 'Red';">Sample</td>
        <td onclick="this.style.backgroundColor = 'Blue';">Data</td>
    </tr>
  </table>
Share:
28,939
Rohan
Author by

Rohan

Developer, UX designer, Builder. Sometimes useful :)

Updated on July 09, 2022

Comments

  • Rohan
    Rohan almost 2 years

    I'm trying to change the background color of an HTML table cell when the user clicks on the cell. Any ideas on how to do this? I have access to the JS Prototype library, so any suggestions for Prototype or straight Javascript would be welcome.