Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check if a JavaScript-Eventhandler has been set?

I've got a JavaScript-function that sets the "onclick"-event of some HTML-Nodes - even if that "onclick"-event has been set before.

How can i check if that event has already been set, so I can extend my function to set the event only on HTML-Nodes where it has not already been set?

like image 796
user4531 Avatar asked Jun 10 '09 15:06

user4531


People also ask

How do you check if an element already has an event listener?

To check if an element has event listener on it with JavaScript, we can call the getEventListeners function in the Chrome developer console. getEventListeners(document. querySelector("your-element-selector")); in the Chrome developer console to select the element we want to check with querySelector .

How do I find an event handler?

If you are looking to see if a specific handler (function) has been added then you can use array. You can examine various properties on the Method property of the delegate to see if a specific function has been added. If you are looking to see if there is just one attached, you can just test for null.

How do you check if there is an event listener in JavaScript?

Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.


3 Answers

Check, like this:

if(typeof someNode.onclick == "function") {
   // someNode has an event handler already set for the onclick event...
}

By the way, if you are using a library, you should say so - if you are, it might be easier/cleaner and would differ based on which library you are using...

like image 94
Jason Bunting Avatar answered Oct 27 '22 13:10

Jason Bunting


It should be possible to detect event handlers attached using <element>.addEventListener() by intercepting calls to the function:

var myListOfAddedEvents = [];

var realAddEventListener = HTMLElement.prototype.addEventListener;

HTMLElement.prototype.addEventListener = function(evtType,fn,cap) {
    myListOfAddedEvents.push(
        {on: this, type: evtType, handler: fn, capture: cap}
    );

    return realAddEventListener.apply(this, arguments);
};

Note: That is untested code and may need some work. I'm hoping this same function will be used by all element types, but I could be wrong. Also it will only work if you can run this bit of code before they start adding events. A copy for attachEvent could be constructed similarly.

like image 31
joeytwiddle Avatar answered Oct 27 '22 15:10

joeytwiddle


<input type="button" id="foo" name="foo" value="bar" />

<script type="text/javascript">
    alert(document.getElementById('foo').onclick); //undefined
</script>

So what you want to do is something like this:

<script type="text/javascript">
    if (undefined == document.getElementById('foo').onclick)
    {
        // Add event handler
    }
</script>
like image 41
Philippe Gerber Avatar answered Oct 27 '22 15:10

Philippe Gerber