1

Let's say i have attached an event to an input, like this :

$('#input-address').on('change',function() {
// do something
});

Later in my code, i need to check if the 'input-address' has a change listener and call it manually. Something like this :

if ($('#input-address').hasOnChangeEvent) $('#input-address').callOnChangeEvent();

I know i can do some workaround to achieve this, would be easier for only one time task. But what i'm trying to achieve is a way to do it generically and apply for multiple inputs. Is this possible ?

Thanks in advance !

delphirules
  • 6,443
  • 17
  • 59
  • 108

1 Answers1

2

You seem to be over-thinking the problem a little. You don't need to check if the event handler is bound at all. If you trigger the event and a handler is bound, then your logic will execute. If you trigger the event and no handler is bound, nothing will happen.

Therefore you can just raise the event using change() or trigger('change') regardless.

$('#input-address').trigger('change');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339