I'm trying to cut down the repetitive code in JQUERY but can't figure out how to call my function from the "on-> click" function.
My function looks like this:
function myCheckAll(div, closer, finder){
console.log(div+closer+finder);
$(div).closest(closer).find(finder).prop('checked', this.checked);
}
And I'm trying to call it with something like this
$("#check_all_0").on('click', myCheckAll("#check_all_0", "table", "input#val0"));
My problem is it doesn't work. What I don't understand is:
1) I'm defining the function outside of the document-ready brackets. Is this where I'm supposed to define it?
2) The console log is being called as soon as I load the page. Without clicking the target, and it doesn't call the function if I click the target.
I'm new to jquery and am probably doing something completely wrong, but would appreciate any help. Thanks.
"input#val0"don't do that. Change to"#val0"the other way is slower and redundant.finderis a DOM ID there is no need for all the other jQuery.$("#val0").prop('checked',this.checked);is all you need.