How to attach event with object for resize & click

74 Views Asked by At

I am using jQuery version 1.4.1. I try to attach resize & click events this way but it is not working.

var $els = [];
$els.window = $(window);
$els.window.bind('resize', getNewWindowCorner());
$els.toggleUPSButtons.bind('click', toggleUPSOverlay());

Help would be appreciated. Thanks.

3

There are 3 best solutions below

0
On BEST ANSWER

If you are using $els.window.bind('resize', getNewWindowCorner()); function executed immediately,so remove bracket

$els.window.bind('resize', getNewWindowCorner);
$els.toggleUPSButtons.bind('click', toggleUPSOverlay);
0
On

You want to give the reference of the function to the handler, not the returned value. Remove the trailing brackets:

$els.window.bind('resize', getNewWindowCorner);
$els.toggleUPSButtons.bind('click', toggleUPSOverlay);
0
On

One different way is as follows if you want to call the functions yourself.

var $els = [];
$els.window = $(window);

$els.window.bind('resize', function(){
 getNewWindowCorner();
});

$els.toggleUPSButtons.bind('click', function(){
 toggleUPSOverlay();
});