Using .live() with .each()
Hello all,
I have a function that I apply to every anchor tag in a div called listnav. I do so like this:
- $('.listnav a').each(function(i, el) {
- $(el).click(function(ev) {
- ev.preventDefault();
- var href = $(this).attr('href');
- $('.listnav a').removeClass('hit');
- $(this).addClass('hit');
- load(href);
- });
- });
This,among other things, causes a function called load to be called on the href of the anchor. This works fine. Howver I want to use the live() function with this as I am going to dynamically change the links in the listnav div. I tried doing it like this :
- $('.listnav a').each(function(i, el) {
- $(el).live('click',function(ev) {
- ev.preventDefault();
- var href = $(this).attr('href');
- $('.listnav a').removeClass('hit');
- $(this).addClass('hit');
- load(href);
- });
- });
But that does not work. My question is how would I go about changing this function so that the actions are applied to any new anchors added to the div dynamically? I'm assuming I have a syntax error of some kind here but am just not sure what I should change. Any advice is appreciated, thanks!