views:

267

answers:

6

I am creating a dynamic table in which each row has a group of radio buttons placed in different columns. I want to change the cell background color when a radio button is clicked.I am constructing the table using the code snippet below. How to identify the radio button in javascript, so that I can set the cell with an appropriate background color.

<table>
    <c:forEach>
        <tr>
            <c:forEach>
                <td><input type="radio" value="" /></td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>
A: 

you should try and set a unique id for the input field when creating it (e.g. id="theradiobutton")

Then you can reference it easily using DOM methods!

Ganesh Shankar
you could also add an onClick() event to each radio button to do what you require. But it's better to have a unique identifier...
Ganesh Shankar
A: 

You need to set the id or use a fake class name.

Later I should use jquery for accessing and changing them. http://www.google.es/search?rlz=1C1GGLS_esES361ES361&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=radio+button+jquery

Pablo Castilla
A: 
var myform = document.forms['myform'];

for ( var i=0; i < myform.elements; i++ )
    if ( myform.elements[i].type == 'radio' ) ...do your stuff

Hope it helps.

aefxx
this is ok but if you have more than one radio button and only want to set the background colour on ONE of these, it'll start getting messy...
Ganesh Shankar
+1  A: 

Use dojo or jQuery to select the radioButton node, then use CSS filter expressions to set the td tag (parentNode dom node) to whatever color you want.

Example using dojo :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Dojo selectors example</title>
            <script type="text/javascript">
                var djConfig = {
                    parseOnLoad: true,
                    isDebug: true,
                    locale: 'en-us'
                };
            </script>
            <script type="text/javascript" src="http://o.aolcdn.com/dojo/1.4.1/dojo/dojo.xd.js"&gt;&lt;/script&gt;
        <script type="text/javascript">

            dojo.addOnLoad(function() {
                // Get all the radioButtons that are inside a <td> tag
                dojo.query("td > input").forEach(function(node, index, array){
                    var td = node.parentNode;
                    dojo.addClass(td, "red");
                    dojo.connect(td, "onclick", function(){dojo.toggleClass(td, "white");});
                });
            });


        </script>
        <style type="text/css">
        .red { background-color : red; }
        .white { background-color : white; }
    </style>
    </head>
    <body>
        <form id="form1">
        <table>
            <tr>
                <td><input type="radio" value="" /></td>
            </tr>
            <tr>
                <td><input type="radio" value="" /></td>
            </tr>
            <tr>
                <td><input type="radio" value="" /></td>
            </tr>
        </table>
        </form>
</body>
</html>

I'll leave it up to you to correct the radiobutton's behaviour...

Philippe
A: 

It's simple with jQuery. Here's an SSCCE, copy'n'paste'n'run it.

<!doctype html>
<html lang="en">
    <head>
        <title>Test</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
        <script>
            $(document).ready(function() {
                $('#tableId input[name=row]').click(function() {
                    $('#tableId tr').removeClass('selected'); // Reset.
                    $(this).parents('tr').addClass('selected');
                });
            });
        </script>
        <style>
            tr.selected {
                background: #ffc;
            }            
        </style>
    </head>
    <body>
        <table id="tableId">
            <tr><td><input type="radio" name="row" value="row1">row1</td></tr>
            <tr><td><input type="radio" name="row" value="row2">row2</td></tr>
            <tr><td><input type="radio" name="row" value="row3">row3</td></tr>
        </table>
    </body>
</html>

Just translate the table back into the dynamic flavor you've had with JSP/JSTL.

BalusC
This is the approach I would take, but I would use the built-in jquery radio selector since it is more performant (and requires less tweaking of the html). So it would be $("#tableId :radio").click(function() ...);
ckramer
@ckramer: I know this one. However, he might have more radiobuttons in the same table for other purposes. I would otherwise "accidently" have used `input[type=radio]` ;) Oh, if you like this approach, why don't you just upvote? :o
BalusC
A: 

Without getting too fussy about it, this is really pretty simple. You don't need to identify the radio button, just call an event handler and pass the instance of the button with it:

<td><input type="radio" value="" onclick="colorMyCell(this)" /></td>

and the handler:

function colorMyCell(inp) {
    // get reference to the row
    var tr = inp.parentNode.parentNode;
    // put the TD children of the row into an array
    var cells = tr.getElementsByTagName("TD");
    // bgcolor all the other cells in that row white
    for (var i=0; i<cells.length; i++) {
        cells[i].style.backgroundColor = "#ffffff";
    }
    // now bgcolor the selected cell differently
    inp.parentNode.style.backgroundColor = "#ffffcc";
}

Note that this is just a quick and dirty example of how to do this. You would want to take more care with it (make sure inp.parentNode.parentNode really is a TR, if not find a better way to work your way up the tree to find the actual first-ancestor TR), as well as using CSS classNames instead of directly setting background colors, and so on.

Robusto