Achaius
Achaius

Reputation: 6124

How to find the parent element using javascript

I want to change the background color of the table cell when radio button inside the cell is clicked.

<table>
  <tr>
    <td align="center">  
      <input type="radio" value="foo" 
        onclick="this.parentElement.style.background-color='red';" />
     </td>
  </tr>
</table>

How to get the parent element reference?

Upvotes: 52

Views: 110454

Answers (2)

ryanulit
ryanulit

Reputation: 5001

Use the change event of the select:

$('#my_select').change(function()
{
   $(this).parents('td').css('background', '#000000');
});

Upvotes: 3

Yuval Adam
Yuval Adam

Reputation: 165340

Using plain javascript:

element.parentNode

In jQuery:

element.parent()

Upvotes: 129

Related Questions