[ 4 posts ]

Dave Lozier

  • Username: Dave
  • Joined: Wed Jun 03, 2009 12:20 pm
  • Posts: 12
  • Offline
  • Profile

Event Delegation with nested elements?

Post Posted: Sun Aug 21, 2011 1:13 pm
+0-
Does anyone have an example of using Event Delegation with nested elements?

Example:

Code:
<table id="mylist"><tr id="msg_2"><td>asdf asdf asdf <a href="#" id="del_2">delete</a></td></tr></table>
<script>
var onMyClick = function (event, matchedEl, container) {
alert(matchedEl.id);
}
Event.delegate("mylist","click",onMyClick,"a");
Event.delegate("mylist","click",onMyClick,"tr");
</script>


If the anchor tag is clicked on I want to prevent the click event for TR from being called. Any help or insight is greatly appreciated. :)

Mitch

  • Username: Mitch
  • Joined: Thu Aug 04, 2011 9:10 am
  • Posts: 16
  • Offline
  • Profile
Tags:

Re: Event Delegation with nested elements?

Post Posted: Sun Aug 21, 2011 1:19 pm
+0-
You have to cancel your event.

However the following is a little bit nicer.
Code:
<table id="mylist">
  <tr id="msg_2">
      <td>
         <span>asdf asdf asdf</span>
         <a href="#" id="del_2">delete</a>
      </td>
  </tr>
</table>
<script>
var onMyClick = function (event, matchedEl, container) {
alert(matchedEl.id);
}
Event.delegate("mylist","click",onMyClick,"a");
Event.delegate("mylist","click",onMyClick,"td>span");
</script>

Dave Lozier

  • Username: Dave
  • Joined: Wed Jun 03, 2009 12:20 pm
  • Posts: 12
  • Offline
  • Profile
Tags:

Re: Event Delegation with nested elements?

Post Posted: Sun Aug 21, 2011 1:46 pm
+0-
Thanks Mitch. I've tried the stopEvent(e) method but it did not help. I still get two alerts with the click on the anchor tag.

A more detailed example of the table:
Code:
<table id="mylist">
  <tr id="msg_2">
      <td><img src="/userPic.jpg"></td>
      <td>
         <div>UserName
         <a href="#" id="del_2">delete</a>
         </div>
         <div class="msgSnippet">asdf asdf asdf asdf asdf</div>
      </td>
  </tr>
</table>
<script>
var onMyClick = function (event, matchedEl, container) {
Event.stopEvent(event);
alert(matchedEl.id);
}
Event.delegate("mylist","click",onMyClick,"a");
Event.delegate("mylist","click",onMyClick,"tr");
</script>

Dave Lozier

  • Username: Dave
  • Joined: Wed Jun 03, 2009 12:20 pm
  • Posts: 12
  • Offline
  • Profile
Tags:

Re: Event Delegation with nested elements?

Post Posted: Sun Aug 21, 2011 2:01 pm
+0-
I've actually got this to work. Here's what I came up with:

Code:
<table id="mylist">
  <tr id="msg_2">
      <td><img src="/userPic.jpg"></td>
      <td>
         <div>UserName
         <a href="#" id="del_2">delete</a>
         </div>
         <div class="msgSnippet">asdf asdf asdf asdf asdf</div>
      </td>
  </tr>
</table>
<script type="text/javascript">
(function() {
    var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;
    var onMyClick = function (event, matchedEl, container)
    {
        var tar = Event.getTarget(event);
        if (tar.tagName.toLowerCase() == 'a')
        {
            alert(tar.id);
        }
        else if (matchedEl.tagName.toLowerCase() == 'tr')
        {
            alert(matchedEl.id);
        }
};
Event.delegate("mylist", "click", onMyClick, "tr");
})();
</script>
  [ 4 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum