I have seen other questions similar to this one, but I cant seem to find the answer i'm looking for.
Is there any way of passing parameters into a named function in a JQuery event listener?
For example, I know I can do this
$('#myelement').on("change", function(){
var value = $(this).val();
myFunction(value);
});
But is there any way to just pass the function name into the event listener instead?
Something like this
$('#myelement').on("change", myFunction($(this).val()));
I thought it would be straight forward to be honest, but I can't seem to find a way to do it.
$(this)as an argument this way. It does not refer to your event (refers to higher namespace).myFunction.onshould be a function. The way you're trying to do it, the argument would be the return value of the function instead. And the function would be called once, instead of every time a'change'event was fired. If you don't like the syntax, you could dovar handleChange = function() { var value = $(this).val(); myFunction(value); }; $('#myelement').on("change", handleChange);.