52

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?

2
  • 3
    parentElement is a weird and totally pointless IE-only property. The spelling you are looking for is parentNode. Commented Mar 16, 2010 at 12:59
  • 1
    It would be more beneficial to post the real HTML output, not the Java-flavoured tags. h:selectOneRadio could quite easily get converted to some nested HTML tags and selecting the immediate parent wouldn't work. Commented Mar 16, 2010 at 13:00

2 Answers 2

129

Using plain javascript:

element.parentNode

In jQuery:

element.parent()
Sign up to request clarification or add additional context in comments.

2 Comments

I tried like this but its not working <h:selectOneRadio value="#{book.price}" onclick="this.parent().css('background', '#FEDA46');">
this in your case must be a jQuery element. try $(this).
3

Use the change event of the select:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.