Single onClick on Parent instead of on each child?

Discussion in 'Web Design and Development' started by jschulbach, Jul 19, 2010.

  1. jschulbach macrumors newbie

    Joined:
    Jun 29, 2010
    #1
    Is it possible to have an onClick on a div that has multiple children then query the onclick event to see which child was clicked? I could just put an onclick on each child div but it seemed redundant if I could get away with just one. I tried sending the event as an argument to my function. When I alert the target I get the id of the parent if it is clicked but the alert when I click a child is blank.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    It's called bubbling, or event propagation, or event delegation. Those terms should help you find articles and tutorials for this. I've used them myself and are very handy.
     
  3. jschulbach thread starter macrumors newbie

    Joined:
    Jun 29, 2010
    #3
    I knew about the basic concept of bubbling and propagation but couldn't seem to find what I was looking for searching for those. Searching for event delegation brought me to this http://cherny.com/webdev/70/javascript-event-delegation-and-event-hanlders which is exactly what I needed. Thanks for the help!
     

Share This Page