tags:

views:

82

answers:

4

So i get the gist of $.delegate and I know why it's doing what it's doing, but I'm wondering if there is a work around.

I have link elements that contain spans like so:

<a href='#'>
    <span>Person Name</span>
    <span>Person Info</span>
</a>

I use the following code in jQuery for event delegation:

containerElement.delegate('click','a',function(){...});

The trouble is that this only triggers when I click on white space not occupied by a span. I know it does this because delegate simply compares the event target to 'a' to check if it should fire the delegate, however I want to include the spans as well, pretty much anything inside the <a>...</a>

what do?

+2  A: 

you have written it the other way around. the first argument is the selector and the second one is the event name

$('body').delegate('a', 'click', function() { alert('hi'); });

XGreen
Your incorrectness not withstanding, that doesn't even begin to answer my question.
Master Morality
@Master it kind of does, have you tested it? I just did and it triggers for me just fine even if I'm clicking in a span. Perhaps you need to word your question better if that's not what you're looking for?
anomareh
@Master, I tested it as well and when you put the arguments in the correct order it works fine. Clicking the text within the span tags triggers the event.
Erikk Ross
ah, the function is overwritten by my validation plugin... which uses the syntax I described. I wonder how to fix that... that said it uses $.is() to determine if the event target matches the selector. so I guess a better question would be what do I pass to $.is() to make it match the 'a' or anything inside it.
Master Morality
@Master check my answer.
anomareh
A: 

Have you tried jquery live?

http://api.jquery.com/live/

It's built in delegation.

vinhboy
A: 

Like, vinhboy suggests, I'd use

$("a").live("click", myFunction);

function myFunction()
{
   alert("Hello");
}

In fact, give your a tags a class such as "myLink" and then perform on that

$("a.myLink").live("click", myFunction);

function myFunction()
{
   alert("Hello");
}
Graeme
Read: http://api.jquery.com/delegate/ -- Delegate is a shortcut to `each` > `live`. Not the other way around.
anomareh
+1  A: 

Based on your comment I think you're looking for the following:

target.is('a, a > *');

.. or something similar.

anomareh