views:

32

answers:

1

Im trying to create an event for each (a) element in a list (ul). But im doing something wrong

function EnableAjaxOnMenu(ElementID, TagName) {

    var elm = jQuery("#" + ElementID).children(TagName).click(function () {

        GetPageByUrl(jQuery(this).attr("href"));
        //ChangeSelectedMenuItem(this);
        return false;
    });

}

Does anyone know what im doing wrong here, as far as I can see it won't even create an event?

+2  A: 

If you're passing the <ul> ID, then you'll need .find() instead of .children() to find the <a> elements within, since they're not direct children, like this:

jQuery("#" + ElementID).find(TagName).click(...);

Or, like this:

jQuery("#" + ElementID + " " + TagName).click(...);
Nick Craver
Just beat me to it! `children( )` -> selects against direct descendants. `find( )` -> selects against all descendants.
ajm
Thanks a lot, it was exactly what I was looking for:) Is there anything I should do to check the question as answered?
Alex Sleiborg