0

I'm displaying and hide set of radio buttons using jQuery. What I'm doing is I'm showing all the radio buttons and then I'm hiding it. Why can't I just display on the relative radio buttons? I'm displaying the radio buttons based on the year selected. Please check the fiddle so that you can get enough context.

My code is

$('.ncbclaim-slider-noclaim input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').parents('.claim-details-tooltip').show();
$('.ncbclaim-slider-noclaim input:radio[name="form.details.insurancePolicyToTransfer.noClaimBonusInBPS.noClaim"]').not(':lt('+(noNcbYear + 1)+')').parents('.claim-details-tooltip').hide();

http://jsfiddle.net/5ept4r74/42/

Can I have it in the same and one line code? I'm not able to figure it out how to do it.

2
  • Your code is very hard to follow. What I normally do when I have an issue like this and want to ask a question on Stack Overflow is create an alternate less convoluted example. Maybe it has just 2 years and you don't have your insane variable names. That code is making my eyes hurt, very hard to follow. Dec 19, 2014 at 6:47
  • take a look at end() api.jquery.com/end
    – charlietfl
    Dec 19, 2014 at 7:02

1 Answer 1

0

Try this:

Demo

$('.ncbclaim-slider-noclaim').addClass('dontshow');
$('#eForm_registeredDate').change(function () {
    $('.ncbclaim-slider-noclaim').removeClass('dontshow');
    var currentYear = new Date().getFullYear();
    var registeredYear = $(this).val();
    $('.ncbclaim-slider .radio-box').show();
    if ($(this).val() != '0') {
        var noNcbYear = currentYear - registeredYear;

        $('input:radio').filter(function(i,e){
            if(i<noNcbYear+1){
                $(e).parents('.claim-details-tooltip').show();
            } else {
                $(e).parents('.claim-details-tooltip').hide()
            }
        })

    } else {
        $('.ncbclaim-slider-noclaim').addClass('dontshow');
    }
});
3
  • Thanks Mangesh. I'm sorry. I don't understand the filter function which you've written. I'm glad that it's working. Could you please explain me a little bit? Dec 19, 2014 at 7:14
  • Reduce the set of matched elements to those that match the selector or pass the function's test. alternatively you can use .each() method as well instead of filter Dec 19, 2014 at 7:24
  • Thanks Mangesh. Accepted your answer Dec 19, 2014 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.