Take the 2-minute tour ×

I have html like this:

<ol id="test-ol">
        <a href="http://example.com">
            <span class="myspan">test</span>
            not in span
            <span class="myspan">test2</span>

and the following JavaScript (Prototype):

$$('#test-ol a').each(function(narrowByElement){

onToolbarClick = function(event){
    var href = Event.element(event).readAttribute('href');

Clicking on the non-span part within the anchor tag would log the right href, but clicking on the "test" part within the span would return null.

So how can I bind my click function to all the elements within my anchor tag?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Try this:

Event.on('test-ol', 'click', 'a', onToolbarClick);


onToolbarClick = function(event, element){
    var href = element.readAttribute('href');

For more details, check out the docs for Event.on, specifically the descriptions for the selector and callback parameters.

share|improve this answer
I suggest that bind is not needed since onToolbarClick doesn't use it's context and there is no indication that this is in use at the time of binding. In the OP's code the use of this might be a confusion with jQuery's $.each which sets the context to each element, here it will be undefined or window. –  clockworkgeek Mar 13 '12 at 11:39
@clockworkgeek You're probably right. I've updated my answer to remove the bind comments to make my answer simpler. –  dontGoPlastic Mar 14 '12 at 3:47
thanks, that kinda did the trick. –  MOmann Mar 14 '12 at 10:28
But it should probably be Event.on($('test-ol'), 'click', 'a', onToolbarClick); –  MOmann Mar 14 '12 at 10:38
@MOmann Yes, you're correct - either an element or an id string, not necessarily a selector. I've edited my answer. Glad to help. –  dontGoPlastic Mar 15 '12 at 21:03
add comment
onToolbarClick = function(event){
    var href = this.getAttribute('href');

For prototype 1.6.0 and higher.

share|improve this answer
this.getAttribute is not a function - Prototype 1.7 –  MOmann Mar 12 '12 at 20:17
Ah, you bound it to event. Then readAttribute should work. –  kirilloid Mar 12 '12 at 21:18
I'm afraid no - it doesn't. –  MOmann Mar 12 '12 at 21:34
Ok, which version of prototype do you use? –  kirilloid Mar 12 '12 at 21:41
add comment

Your Answer


