Check if event exists on element [duplicate]

Asked
Active3 hr before
Viewed126 times

6 Answers

elementcheckevent
90%

you can use it like this, Stack Overflow Public questions & answers , GitLab launches Collective on Stack Overflow ,You can iterate over it using each or for ex. check the length for validation like:

$('body').click(function() {
   alert('test')
})

var foo = $.data($('body').get(0), 'events').click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each(foo, function(i, o) {
   alert(i) // guid of the event
   alert(o) // the function definition of the event handler
});
load more v
88%

Is there a way to check if an event exists in jQuery? I’m working on a plugin that uses custom namespaced events, and would like to be able to check if the event is bound to an element or not.,To check for events on an element:,A more complete way to check, that includes dynamic listeners, installed with $(document).on,I wrote a plugin called hasEventListener which exactly does that.

Answer #1:

$('body').click(function() {
   alert('test')
})

var foo = $.data($('body').get(0), 'events').click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each(foo, function(i, o) {
   alert(i) // guid of the event
   alert(o) // the function definition of the event handler
});
load more v
72%

Possible duplicate: How to check whether dynamically attached event listener exists or not?,How to check if an element has event listener on it, if I use an inline function on it like the code below? Because I have a function that recalls the function and add the event listener, but it cause to have duplication event listener causing it to trigger a function twice. How can I check it so I can prevent it to add an event listener if is it already exist? ,There is no JavaScript function to achieve this. However, you could set a boolean value to true when you add the listener, and false when you remove it. Then check against this boolean before potentially adding a duplicate event listener.,There's a handy trick in these situations: use a setTimeout with 0 milliseconds. This will cause your JavaScript to yield to the browser (so it can perform its rendering, respond to user input and so on), but without forcing it to wait a certain amount of time:

How to check if an element has event listener on it, if I use an inline function on it like the code below? Because I have a function that recalls the function and add the event listener, but it cause to have duplication event listener causing it to trigger a function twice. How can I check it so I can prevent it to add an event listener if is it already exist?

for (var a = 0; a < formFieldInput.length; a++) {
   if (formFieldInput[a].hasAttribute("name") && formFieldInput[a].attributes.title.value !== "Valid Until") {
      formFieldInput[a].addEventListener("click", function(event) {
         toggleFieldList(event, "show");
      });
   }
load more v
65%

Here, we'll take a look at how to use an anonymous function to pass parameters into the event listener. ,More details on the use of this in event handlers,A case-sensitive string representing the event type to listen for., This example demonstrates how to use addEventListener() to watch for mouse clicks on an element.

target.addEventListener(type, listener);
target.addEventListener(type, listener, options);
target.addEventListener(type, listener, useCapture);
target.addEventListener(type, listener, useCapture, wantsUntrusted); // wantsUntrusted is Firefox only
load more v
75%

To check if event exists on element in jQuery, check for the existing events on the element. ,Here, I have set the div −,How to check if onClick exists on element in jQuery?,How to check if element exists in the visible DOM?

Here, I have set the div −

<div id="demo">
     This is demo text. Click here!
</div>
load more v
40%

Some primitive machines do handle input like that. A step up from this would be for the hardware or operating system to notice the keypress and put it in a queue. A program can then periodically check the queue for new events and react to what it finds there.,As an example, the following program displays a bar and sets up event handlers so that dragging to the left or right on this bar makes it narrower or wider:,A better mechanism is for the system to actively notify our code when an event occurs. Browsers do this by allowing us to register functions as handlers for specific events.,Try not to do such things unless you have a really good reason to. It’ll be unpleasant for people who use your page when expected behavior is broken.

A better mechanism is for the system to actively notify our code when an event occurs. Browsers do this by allowing us to register functions as handlers for specific events.

<p>Click this document to activate the handler.</p>
<script>
  window.addEventListener("click", () => {
    console.log("You knocked?");
  });
</script>
load more v

Other "element-check" queries related to "Check if event exists on element [duplicate]"