Search code examples
javascriptscopeprototypejsthis

Prototype.js, calling invoke observe click, trying to get the element clicked. Confusion about scope


Is there a way, using 'invoke', with 'observe', and 'click' as arguments, to access the item that 'invoke' is currently iterating through?

I am doing this:

1.

$$('.item').invoke('observe', 'click', function(event) {
    // I want to access the .item that was clicked in here
});

I can do

2.

$$('.item').first().observe('click', function(node) {
    // I can access the .item right here with 'node'
})

I can even do

3.

    $$(".item").each(function(node) {
      node.observe('click', function(event) {
        // use 'node' here for the .item clicked
    });

But I want to know if there is a way to do #1 (the shorthand way), while still accessing the element that was clicked.

Is there a way to do that?


Solution

  • Found my answer.

    What I was looking for was Event.element(event), which returns the element in which the event was called.

    I also should note that in order to use prototype methods on event.element, you need to wrap it in the prototype selector like this:

    var element = $(Event.element(event))