views:

279

answers:

3

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

<table>
 <tr>
  <c:forEach items="#{books}" var="book">    
   <td align="center">                                                
    <h:selectOneRadio value="#{book.price}" onclick="this.parentElement.style.background-color='red';">
     <f:selectItems value="#{books.getDetails()}" />
    </h:selectOneRadio>
    </td>
  </c:forEach>
 </tr>
</table>

How to get the parent element reference?

+7  A: 

Using plain javascript:

element.parentNode

In jQuery:

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

Use the change event of the select:

$('#my_select').change(function()
{
   $(this).parents('td').css('background', '#000000');
});
ryanulit
He tagged it as jQuery?
ryanulit
A: 

The JSF h:selectOneRadio component itself already renders a table. You need to get the parent td of the parent td. E.g.

<h:selectOneRadio value="#{book.price}" onclick="highlight(this)">

with

function highlight(radio) {
    radio.parents('td').parents('td').css('background', '#000000');
}

Tip: when using a server side view technology / MVC framework, better look at its generated HTML output when writing JS/jQuery code. Rightclick page in browser and view source and it will be much clearer what JS/jQuery actually sees and traverses.

BalusC
I tried this but i couldn't able to get the parent element bgcolor
Kalpana
Check the generated HTML output and write/alter JS/jQuery code accordingly. If you stucks in understanding how HTML and jQuery fits in each other and thus want assistance, then update your question with (relevant parts of) the generated HTML output.
BalusC