views:

273

answers:

2

I have a table that contains multiple rows. Each row contains 5 columns (or 5 TDs). Inside of the first TD is a text field and a select box. The 4 other TDs each contain a table that contains a set of radio buttons.

<tr bgcolor=#ffffff>
<td valign=center>
  <select name=player1>
  <option>0</option>
  <option>1</option>
  </select>
  <input type="text" id="PlayerLocal1" name=p1name size=20>
</td>
<td>
  <table border=1>
    <tr>
      <td>
    <table border=0 cellspacing=0 cellpadding=0>
        <tr>
            <td><input type=radio name=p1o1 value="1B">1B
            <td><input type=radio name=p1o1 value="FO">FO
        </tr>
    </table>
      </td>
    </tr>
  </table></td></tr>

Basically I want the radio buttons in each of the 4 other TDs disabled unless the user inputs a value in the text field OR selects a value in the select field.

I was thinking of adding a class to each TR, then somehow traversing each TD that isn't the first TD and removing the disabled attribute (to enable it), but I can't wrap my head around how create the conditional statement or whether I need to use Parents() or Siblings() or something else to traverse.

Quick Clarification: My table has multiple rows and multiple columns (I only showed 2 of the TDs to save space, but the other 3 TDs looks just like the 2nd). For instance, player1 and p1name1 will be player2 and p2name2 in the second row. So, if the text/select is changed in the FIRST row, it shouldn't enable all radio buttons in ALL rows -- only radio buttons the FIRST row.

Any help is appreciated!

A: 

You could put a div around those TDs which you can hide using javascript. A search on google will help you how to hide divs :). Good luck!

Younes
I don't want to hide anything, just disable the form element.
tresstylez
+1  A: 
$('input:radio').attr('disabled', true);

$('select[name=player1], input[name=p1name]').change(function(){
   $('input:radio').attr('disabled', false);
});

I would suggest putting quotes around attributes though, like name="p1name" rather than name=p1name. Crazy things can happen when you don't.

Also, when you give elements id's or classes, you can be a bit more precise with which elements are selected and which aren't. Say, for example, you only wanted one of the radio buttons to be disabled, you could do that easily, whereas without the id, it's much more difficult.

As per request:

give the selects and input's a class, like

<select class="affector"></select>
<input type="text" class="affector" />

and you can traverse like this:

$('.affector').change(function(){
   $(this).parents('tr').find('input:radio').attr('disabled', false);
});

If you were to have more radio buttons somewhere in the <tr>, and you only wanted to enable a select group, again, having classes would be beneficial. You could substitute the class selector for the "input:radio" in the .find() and only those would be affected.

Addition:

If they''re the only selects and text inputs on the page, you could also forgo adding the classes and use type selectors instead:

$('select, input:text').change(function(){
   $(this).parents('tr').find('input:radio').attr('disabled', false);
});
munch
What if there is more than 1 row of select/text + radio buttons?
Lazarus
This looks like it MAY work, however, how can we GENERALIZE this. My table has multiple rows and multiple columns. For instance, player1 and p1name1 will be player2 and p2name2 in the second row. So, if the text/select is changed in the FIRST row, it shouldn't enable all radio buttons in ALL rows -- only the FIRST row. I'll update the OP (original post) with this info.
tresstylez
The latter half of the answer should work. Once you give the select/input a common class, you're moving up the DOM to the their parent `tr` and then you're finding the radio buttons inside that row and enabling them.
munch
Awesome...both of those solutions worked, although I had to go with adding a class name because there were other selects in the row. One thing though... my select field is dynamically populated with elements based on a result of an AJAX call to a PHP script. For some reason, the .change function does not fire when the select field is populated. The text field works perfect though, and i'm sure a static select box would show the desired behavior as well. Any ideas?
tresstylez
Use jQuery unbind and bind `function addSelectChange(){ $('select').unbind('change'); $('select').bind('change', function(){ //yada yada });}` Call addSelectChange in your ajax success function. It should do the trick. Take a look at jQuery live event too (for when you want to set events for all current and future DOM elements in a later project or something). Unfortunately the change event and a few others aren't yet supported with live. Bind is the next best thing
munch