Get information about the source of an event with jQuery

Zian Choy picture Zian Choy · Sep 3, 2009 · Viewed 43.5k times · Source

A function is bound to an event for many objects. One of those objects fires the event and jQuery kicks in. How do I find out who fired the event inside the JavaScript function?

I've tried using function [name](event){} and poking at event but I get "'data' is null or not an object".

Here's my code right now:

<script type="text/javascript">
    $(document).ready(function() {
    $('.opening').bind("click", function(event) {
         //code to populate event.data since right now, the tags don't hold the info...they will eventually with an XHTML custom namespace
            event.data.building = "Student Center";
            event.data.room = "Pacific Ballroom A";
            event.data.s_time = "9/15/2009 8:00";
            event.data.e_time = "9/15/2009 10:00";

            indicatePreferenceByClick(event);
        });
    function indicatePreferenceByClick(event) {
        alert("I got clicked and all I got was this alert box.");
        $("#left").load("../ReserveSpace/PreferenceByClick", { building: event.data.building, room: event.data.room, s_time: event.data.s_time, e_time: event.data.e_time});
    };
</script>

Answer

user7488658 picture user7488658 · Jan 30, 2017

Using target you can access the control that has caused the current execution/ event. Like this:

$(event.target)