How to find the parent element using javascript

JavascriptJqueryHtml

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

Using plain javascript:

element.parentNode

In jQuery:

element.parent()

Solution 2 - Javascript

Use the change event of the select:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAchaiusView Question on Stackoverflow
Solution 1 - JavascriptYuval AdamView Answer on Stackoverflow
Solution 2 - JavascriptryanulitView Answer on Stackoverflow