| Page 1 of 1 | [ 9 posts ] |
|
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"); |
|
you can only use Yui2 and delegat function?
because i can have your result with Yui3 and event function 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> |
|
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. |
|
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
|
use('console')
then Y.log(object) can be a bit more informative, especially if you are using Chrome or Firefox |
Joe Developer
|
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 |
|
And the answer is:
matchedEl.parentNode.id; thanks to all for the hints. |
Joe Developer
|
Good stuff.
Got Code? Get Assembla
|
|
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 |
| Page 1 of 1 | [ 9 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group