0

I have 10 checkbox in my page and I want some code run after any checkbox checked. look at my code please:

var width = 0;

    $('#checkbox1').change(function() {
       if($(this).is(":checked")) {
       width+=5;
       }
       else{
       width-=5;
       }
   //this is the code that i want it run after any of checkboxes clicked
   if (width <= 25){do something}
   if ( width > 25 && width <= 50){do something}
   if ( width > 50 &&  width <= 75){do something}
   if ( width > 75 && width <= 100){do something}
   });

Is there any way to create a function or something else to run these codes inside every checkboxes and prevent from repeating these codes?

Thanks for answers. but i want to repeat just four "if" at the bottom of the page and "width+=5" or "width-=5" is not repetitive.

1
  • 1
    '#checkbox1' is targeting only one of them. can you add a fiddle with your whole code? Commented Dec 25, 2014 at 16:38

2 Answers 2

2

you can use general selector for checkbox :checkbox refrence

$('input:checkbox').change(function() {
       if($(this).is(":checked")) {
       width+=5;
       }
       else{
       width-=5;
       }
   //this is the code that i want it run after any of checkboxes clicked
   if (width <= 25){do something}
   if ( width > 25 && width <= 50){do something}
   if ( width > 50 &&  width <= 75){do something}
   if ( width > 75 && width <= 100){do something}
   });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer. but i want to repeat just four "if" at the bottom of the page and "width+=5" or "width-=5" is not repetitive.
@user2781069 this needs to be there for every checkbox. this isnt repetition i guess
0

you can bind two event listeners change for the specific checkbox, and click, for all the checboxes

so your non-repetitive code on the selected checbox only

$('#checkbox1').change(function () {
    if ($(this).is(":checked")) {
        width += 5;
    }
    else {
        width -= 5;
    }
}

the repetitive code applied on all the checkboxes

 $('input[type="checkbox"]').click(function() {
           //this is the code that i want it run after any of checkboxes clicked
          if (width <= 25){do something}
          if ( width > 25 && width <= 50){do something}
          if ( width > 50 &&  width <= 75){do something}
          if ( width > 75 && width <= 100){do something}

    });

or use what A.B suggested $('input:checkbox')

2 Comments

Thanks for answer. but i want to repeat just four "if" at the bottom of the page and "width+=5" or "width-=5" is not repetitive.
you can register both events, one to a particular checkbox, and one applied to any checkbox. I've edited an answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.