I seem to get something fundamentally wrong. I have this HTML
<div id="thumbnails">
<a href="#image-0">
<img src="blabla-1.jpg" />
</a>
<a href="#image-0">
<img src="blabla-1.jpg" />
</a>
<a href="#image-0">
<img src="blabla-1.jpg" />
</a>
</div>
and this JavaScript (MooTools library in use)
document.id('thumbnails').getElements('a').each(function(image_link, image_link_index)
{
image_link.addEvent('click', function(evt)
{
if (evt.target.get('tag') == 'a')
{
evt.stop();
console.log('a tag', evt.target);
}
console.log(':-(', evt.target);
});
});
Strangely I never get to that a element. I’m sure I’m misunderstanding something basic here.
You can play around with the code at http://jsfiddle.net/maryisdead/kHBE3/8/
why are you reinventing the wheel? event delegation is not something to be taken lightly – and you should use the built-in event-delegation (since 1.4.1) http://mootools.net/docs/core/Element/Element.Delegation
your code would change to:
where the
relay()pseudo can take any selector you like – eg. a.foo or a[href=#]keep in mind that in 1.2 the event delegation was sort of experimental and somewhat less than perfect – when it came to
mouseover:relay()orfocus:relay(input[type=text])you can get some unexpected results in different browsers – issues addressed in 1.3.2 iirc. Also,changeevents on checkboxes and radios in old ie6/7/8 revert to onpropertychange and may not bubble.in any case, yours fails to come through to the anchor link as the
event.targetas the event itself BUBBLES from the top down. i.e. it will start fromimg > a > thumbnailsbut it won’t raise different events for both – it will be the sameevent.targeton both all 3 elements yet -> this === awhat you can do though is
console.log(this.get("tag") === 'a')); // true– even though the initial target was the child ofthis