I’d like to learn how to better write a more generic method which can be called onload and onclick. This is what I have thus far which works perfectly.
rushJustification($("#rush"));
$("input#rush").click(function() {
rushJustification($(this));
});
function rushJustification(obj) {
var rushInput = obj.is(':checked');
var rushContainer = $("#rush-justification-container");
if(rushInput) {
rushContainer.slideDown(300);
} else {
rushContainer.hide();
}
}
Instead of copying the contents of the
clickhandler, trigger it as well:The previously mentioned method can have some side effects, when other scripts have already bound an event. In your case, you can then use the following code:
It may look weird at first. This feature is possible, because of jQuery’s chainability. The
clickmethod returns the same jQuery collection, namely$('input#rush'). This is then passed as an argument to therushJustificationfunction.