1

I´m looking for all internet and I cannot find a situation like mine.

I have a jsp(lets call it A.jsp) with an import javascript, which has a onDocumemntReady. In that documentReady I bound an click event for a class element of my A.jsp.

Now The issue is that I have another page with a loop where I iterate and include n A.jsp.

So now what´s happening is that I add this n pages on the DOM and after finish the onDocumentReady is call it for everyone. So what I want to do is just create the "click" events once. Looking on google I cannot find a way to check if the event already exist for JQuery 1.11.1. Any idea.

Regards.

paul
  • 12,873
  • 23
  • 91
  • 153

1 Answers1

2

why not to simply "Off" and "On" events.

example:

$(selector).off('click').on('click',function(){//do something});

or If you only want one event per selector, use one:

$(selector).one('click',function(){//do something});

NOTE: For your curiosity, there are plugins avaiable which are supported across required jQuery version. https://github.com/Inducido/jquery-handler-toolkit.js

Vijay
  • 2,965
  • 1
  • 15
  • 24
  • I dont understand, for what for I need the on and off?. – paul Jan 27 '15 at 08:32
  • you need to unbind the event on an element before binding it again on the same element to ensure only one instance of event is bonded. – Vijay Jan 27 '15 at 08:34
  • You should avoid to used `.off()` with only the event names. `[...]When writing code that will be used as a plugin, or simply when working with a large code base, best practice is to attach and remove events using namespaces so that the code will not inadvertently remove event handlers attached by other code. [...]` – t.niese Jan 27 '15 at 08:34
  • remove the event and create it again is a not elegant solution, but might works. Thanks – paul Jan 27 '15 at 08:36
  • Yes I read that plugin as well. But I dont want to add more third parties libraries. Thanks anyway – paul Jan 27 '15 at 08:42