Say when a checkbox is checked/unchecked, I want specific behavior to occur.
Now if there are multiple points in my code where I set a checkbox as checked/unchecked, I have to run through the same logic ever time.
What is a way I can encapsulate the logic that occurs when a checkbox is checked, and just re-use that logic?
My guess is that you’re currently binding click event handlers like this:
To encapsulate that logic, you just need to switch from using an anonymous function, to a function that you can reuse, like this:
How’s that?
I might also take a guess at the fact that the logic you’re writing and rewriting is to wire a “check-all” sort of checkbox to a group of checkboxes. I wrote a jQuery plugin recently to handle exactly this sort of thing.
I never got around to uploading it properly to GitHub – here’s the gist of it. Let me know if you’d like to actually use it, and I can explain its usage – it’s pretty darn simple.