views:

186

answers:

2

I'm trying to accomplish something similar to Wikipedia's History history page, dynamically disabling radio buttons in a series.

Ie... if #4 in group two is selected, then 1-4 of group one are disabled, etc.

I know how to disable them individually or as a group, but I'm not sure how to do it in a series of say 1-4:

Individually:

$("#version_history input[id^=versions_2_3]:radio").attr('disabled',true);

or group:

$("#version_history input[id^=versions_2]:radio").attr('disabled',true);

The inputs are named versions_1_X and versions_2_X, X being some number. 1..2..3.. etc.

My end goal is to feed the function the radio button that's clicked... and then disable all the radio boxes of the opposite group either above the number or below.

+2  A: 

Given the following HTML:

<div id="version_history">
   <div class="version_history_item">
       <input type="radio" name="original">
       <input type="radio" name="update">
       <span>description</span>
   </div>
   ...
</div>

One possible approach is:

function updateVersionRadioButtonAvailability() {

    var versionHistoryElement = $('#version_history'),
        originalIndex = $('input[name=original]:checked', versionHistoryElement).parent('.version_history_item').index(),
        updateIndex = $('input[name=update]:checked', versionHistoryElement).parent('.version_history_item').index(),
        visibleStyle = { visibility: 'visible' },
        hiddenStyle = { visibility: 'hidden' };

    $('.version_history_item', versionHistoryElement).each(function(index) {
        $('input[name=original]', $(this)).css(index > updateIndex ? visibleStyle : hiddenStyle);
        $('input[name=update]', $(this)).css(index < originalIndex ? visibleStyle : hiddenStyle);
    });

}

$(document).ready(function() {
    $('#version_history input[name=original], #version_history input[name=update]').live('click', updateVersionRadioButtonAvailability);
    updateVersionRadioButtonAvailability();
});

Dry-coded so your mileage may vary.

Ken Browning
+1: I see what the question is asking now (*hangs head*), but when I tried your code I found a couple of problems with the indexing. The `versionHistoryElement` needs to be different for each index: for the original it needs `[input=original]` and the same with the update. And therefore cannot be used in the `.each()` function below.
fudgey
tested and fixed
Ken Browning
fudgey: I'm not understanding what you're saying about versionHistoryElement needing to be different for each index. The only bug with the code was in the assignment of `originalIndex` and `updateIndex`. They were both missing a call to the `parent` function.
Ken Browning
Ok, it looks like you fixed it. You were indexing from the `.version_history_item` and I was looking at it from indexing each column separately. Either way works :P
fudgey
+1  A: 

I'm not quite sure if I understand what you are trying to accomplish, so I made this demo. Basically it disables all radio groups with lower version numbers of the selected group.

I don't know if you wanted to disable all of them as there would be no way to click them again, so I added a reset button as well.

Maybe if you clarified a bit more what exactly you want?

fudgey
holden
Yeah, now that I've seen Ken's code I see what you want. I had to adjust a few things to get his code working... I'll make a comment in his post.
fudgey