#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20

    Can an event-handler on an element have a different target than that element?


    Hi
    I have the problem that when I set an event handler like so:
    javascript Code:
    $('#some_table_id tr td').on('click', callback  );

    The callback gets a DOM table cell element as event.target if the user clicks on an empty table cell. However, if the user clicks on a table cell containing an image than event.target will contain a DOM image element eventhough the event handler was set on the table cells. Is there a way to get the table cell as event.target even when the user clicked on an image within that table cell.

    Thanks in advance!
    Stefan
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Assuming you're using jQuery, this will be the original element that the handler was bound to.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    Thanks for the reply!

    I'm not sure whether I understand what you mean. In the code snippet of my original post, I have set an event listener on each table cell. When one of those cells is clicked the target can be the actual html cell element or some element that is within that cell. I want a reference to the DOM object of the table cell that is clicked, not of the image within that cell. I'm not sure what you mean with your solution as I did not bind the listener to a specific cell but to all cells and they all use the same callback function.

    Thanks again.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The variable named "this", as in:
    Code:
    $('#some_table_id tr td').on('click', function() {
      console.log(this);
    });

    Comments on this post

    • Stefan1 agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    Ah like that! Thanks!

    I will try it when I have some time to work on it again. I don't know if in my case I can still use the "this" reference as I think I had the callback function bound to another object.

    Thanks in any case.
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The variable this is always bound to the target element in the callback function, it doesn't matter if the function is defined as part of another object.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo