How to mark next checkbox

Asked

Viewed 290 times

2

From the current checkbox selection, how to mark the next checkbox element and disable all other.

I have this code, which when dialing a checkbox consequently all the others are deactivated, but I need that when dialing a checkbox automatically it marks the next checkbox and deactivates the others.

I tried with next() but did not succeed

$(document).ready(function(){
$('#formNine input:checkbox').click(function(){

var $inputs = $('#formNine input:checkbox')

    if($(this).is(':checked')){
       $inputs.not(this).prop('disabled',true); // <-- disable all but checked one
    }else{
       $inputs.prop('disabled',false); // <--
    }

});

});

  • You need to explain what you want to do better. Want to check the next checkbox or the one that was clicked on? Want to do disabled? This also depends on the HTML structure. You can put an example of your HTML here?

  • Its code, adapted, gives this: http://jsfiddle.net/zLu25z45/ which is basically what a radio button does... I want to help but I don’t understand what it wants.

  • for example I have a <label><input type="checkbox"><input type="checkbox"><input type="checkbox"></label> if I mark the first I want the first and the second to be marked, if I mark the second that is marked the second and third, whenever I mark a checkbox, You know what I mean? I think now I can express myself better

1 answer

3


You can do with next().

Add at the end of if:

$(this).next().prop('checked', true).prop('disabled', false);

Thus remaining:

if($(this).is(':checked')){
   $inputs.not(this).prop('disabled',true); // <-- disable all but checked one
   $(this).next().prop('checked', true).prop('disabled', false);
}else{
   $inputs.prop('disabled',false); // <--
}
  • perfect, now only missing when I uncheck the current uncheck the next() that it marked. But that’s exactly it. Sorry I’m not very good at jQuery

  • It looks like this:if($(this).is(':checked')){ $inputs.not(this). prop('disabled',true); // <-- disable all but checked one $(this). next(). prop('checked', true). prop('disabled', false); }Else{ $inputs.prop('disabled',false); // <- $(this). next(). prop('checked', false). prop('disabled', true); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.