[ 9 posts ]

Roman Stachura

  • Username: stachrom
  • Joined: Wed Feb 10, 2010 12:48 am
  • Posts: 21
  • Twitter: stachrom
  • GitHub: stachrom
  • Gists: stachrom
  • IRC: stachrom
  • Offline
  • Profile

Event delegation parent node

Post Posted: Wed Feb 10, 2010 1:14 am
+0-
Hi all


How do i get the parent node ID of <a> element with event delegation in yui2 ?


The Dom:
Code:

<div id="listtable">
<ul>
<li id="liste1'">
   <a class="show-table" href="#">zeigen</a>
   <a class="edit-table" href="#">bearbeiten</a>
   <a class="delete-table" href="#">löschen</a>
</li>
<li id="liste2'">
   <a class="show-table" href="#">zeigen</a>
   <a class="edit-table" href="#">bearbeiten</a>
   <a class="delete-table" href="#">löschen</a>
</li>
[...]
</ul>
</div>


Javascript:
Code:

  var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event,

var onLIClick = function (event, matchedEl, container) {

   if (Dom.hasClass(matchedEl, "show-table")) {

        //show me the parent id of this event!  --> list1 ... or list2 etc...
   
   }

    if (Dom.hasClass(matchedEl, "edit-table")) {
      
   }

    if (Dom.hasClass(matchedEl, "delete-table")) {
      
   }


};


Event.delegate("listtable", "click", onLIClick, "a");

Kaias26

  • Username: Kaias26
  • Joined: Mon Feb 08, 2010 6:49 am
  • Posts: 5
  • IRC: Kaias26
  • Offline
  • Profile

Re: Event delegation parent node

Post Posted: Wed Feb 10, 2010 2:25 am
+0-
you can only use Yui2 and delegat function?
because i can have your result with Yui3 and event function :roll:

Code:
<script src="http://yui.yahooapis.com/3.0.0/build/yui/yui-min.js"></script>
<script type="text/javascript">
YUI().use('node-base', function(Y) {
   function handleClick(e) {
      var oElement = this._node;
      if (oElement.className == "show-table") {
         alert(oElement.parentNode.id);
       }
      if (oElement.className == "edit-table") {
         alert(oElement.parentNode.id);
       }
      if (oElement.className == "delete-table") {
         alert(oElement.parentNode.id);
       }
   }
    Y.on("click", handleClick, "#listtable a");
});
</script>

Roman Stachura

  • Username: stachrom
  • Joined: Wed Feb 10, 2010 12:48 am
  • Posts: 21
  • Twitter: stachrom
  • GitHub: stachrom
  • Gists: stachrom
  • IRC: stachrom
  • Offline
  • Profile

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 2:30 am
+0-
I do not have to use the delegate function from yui2, but it would fit nicely, if I know how to fetch the parent id from <a> element.

There is most certainly more than on way to solve a problem.

If I have to mix yui3 and yui2. I'll do it.
yui2 is mandatory for data table so there will be yui2 anyway.

Thanks for the reply Kaias26.

Kaias26

  • Username: Kaias26
  • Joined: Mon Feb 08, 2010 6:49 am
  • Posts: 5
  • IRC: Kaias26
  • Offline
  • Profile
Tags:

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 2:41 am
+0-
i don't know how the delegate function works but when i want the parent, i try to get the child node first...
when i have this node it's easy to get the parent...
but i dont know how to process with the dom

i like to use this too :
Code:
for (i in myNode) alert(i + '\n' + myNode[i]);

it's return all properties, methods... in your node.

Joe Developer

  • Username: JoeDev
  • Joined: Sat May 09, 2009 12:54 am
  • Posts: 73
  • Twitter: joe_developer
  • IRC: unomi
  • Offline
  • Profile
Tags:

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 3:05 am
+0-
use('console')

then Y.log(object) can be a bit more informative, especially if you are using Chrome or Firefox

Joe Developer

  • Username: JoeDev
  • Joined: Sat May 09, 2009 12:54 am
  • Posts: 73
  • Twitter: joe_developer
  • IRC: unomi
  • Offline
  • Profile

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 3:12 am
+0-
with yui3
you can do e.currentTarget.get('parentNode')
or you can do e.currentTarget.ancestor('css selector')

With yui2 you should be able to do htmlEl.parentNode

Roman Stachura

  • Username: stachrom
  • Joined: Wed Feb 10, 2010 12:48 am
  • Posts: 21
  • Twitter: stachrom
  • GitHub: stachrom
  • Gists: stachrom
  • IRC: stachrom
  • Offline
  • Profile
Tags:

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 4:35 am
+0-
And the answer is:

matchedEl.parentNode.id;

thanks to all for the hints.

Joe Developer

  • Username: JoeDev
  • Joined: Sat May 09, 2009 12:54 am
  • Posts: 73
  • Twitter: joe_developer
  • IRC: unomi
  • Offline
  • Profile

Re: Event delegation parent node

Post Posted: Thu Feb 11, 2010 5:23 am
+0-
Good stuff. :)
Got Code? Get Assembla

David J Wallace

  • Username: danjah
  • Joined: Wed Jun 02, 2010 5:10 pm
  • Posts: 53
  • Location: Wellington, NZ
  • Offline
  • Profile

Re: Event delegation parent node

Post Posted: Wed Jun 02, 2010 9:56 pm
+0-
I'd like to extend on this, I'm stuck with YUI 2.8.1 (stuck with, haha, YUI 2.8.1 is awesome!!!).

I have a situation where I have a container element with a listener on it, and no matter what is clicked - the container or its children - I need to reliably access the container element in the callback when the event is fired.

When I click on a nestedElement, currentTarget == nestedElement. -- this isn't what I want. Sure, parentNode is fine when I know the nesting depth is 1. In my case it could be 2, or maybe even more (<div id="container"><a><img /></a></div>).

So I tried delegation, using the * selector; ye.delegate('container', 'click', this.handleSelection, '*');, and where it works nicely when a nested element of any depth is selected, it surprised me when it did not fire at all when I selected the container itself. I figured maybe it might at least return the 'container' arg as just being itself.

Is the ability to return the 'container' arg as itself when it is itself clicked a configurable option? Is it possible? Is there another solution?

Many thanks, hope this post gets picked up!


cheers,
d
  [ 9 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