jquery - Pass an argument to callback function -
i want pass argument directly function in jquery.
i used
$(this).prev()
to access previous argument. want access previous argument anytime want, without prev method. current code below
$(".a").click(function(){ $(this).next().slidetoggle(1000,function(){ $(this).prev().css("background-color","green"); }); });
use closure merge event object , element single argument list:
$(".a").click(function(eve){ (function ( e, p ) { $(e.target).next().slidetoggle(1000,function(){ $(p).css("background-color","green"); }); })( eve, $(this).prev() ); });
you may opt not deliver event object @ all:
$(".a").click(function(eve){ (function ( n, p ) { $(n).slidetoggle(1000,function(){ $(p).css("background-color","green"); }); })( $(this).next(), $(this).prev() ); });
of course, actual callback function can factored out of event handler registration:
function myclickcallback ( n, p ) { $(n).slidetoggle(1000,function(){ $(p).css("background-color","green"); }); } //... $(".a").click(function(eve){ myclickcallback ( $(this).next(), $(this).prev() ); });
that makes more sense in context of triggering action supposed have effect elsewhere. such behaviour no means recommended practice - non-local effects of user interaction in fact violate ui design recommendations:
function myclickcallback ( n, p ) { $(n).slidetoggle(1000,function(){ $(p).css("background-color","green"); }); } //... $(".a").click(function(eve){ myclickcallback ( $(some_other_element).next(), $(some_other_element).prev() ); });
demo
this fiddle contains poc of idea simplified callback function.
Comments
Post a Comment