views:

45

answers:

1

I am performing a selection on various radio buttons whose class starts with the name radio-selection and has some number at the end (e.g. radio-button1, radio-button2, etc). If the radio button is clicked, certain fields contained with that radio button need to be enabled or disabled. The good part is that the class of the that needs to be enabled matches the radio button # (e.g. radio-option1, radio-option2, etc).

So far, I have this...

$('input:radio[class^=radio-selection]').live('click', function () {
    enableDataGroup($(this).siblings('.radio-option1'));
    disableDataGroup($(this).siblings('.radio-option2'));
});

I just wanted to prove enabling and disabling works (those are my own functions as the enable and disable can become a bit complicated), but now I need to somehow parse off the numeric value off the radio button that is clicked and enable that data group, and disable all others that don't match. Here is the pseudo-code for what I want to do. Unfortunately, I'm not sure what to search for in Google that will teach me how to parse off and add those numeric values.

$('input:radio[class^=radio-selection]').live('click', function () {
    var i = // Parse off the number off the selector of $(this).
    enableDataGroup($(this).siblings('.radio-option' + i));
    // Disable every other .radio-option[X] where [X] != i
});
+1  A: 

It's generally easier to apply a bulk action to everything first and then just enable the one that you actually want to be enabled.

$('input:radio[class^=radio-selection]').live('click', function () {

    // Disable all controls first by the class
    $('input:radio[class^=radio-option]').attr('disabled', 'disabled');

    // Replace the class text with nothing to get your integer
    var i = parseInt($(this).attr("class").replace("radio-selection", ""));
    enableDataGroup($(this).siblings('.radio-option' + i));

});
GenericTypeTea
@GenericTypeTea This is putting me on a right path. There is one error in your code (I edited it). Also, I am having trouble with the selectors. The div.radio-option[#] class is not the only class on the div. So when I use the [class^=radio-selection] I am getting back an empty set. Any way to fix this?
JasCav