Thursday, October 2, 2008

[jQuery] Re: if ($(event.target).is('a.edit')) with img

I think u should look at Live Query Plugin.

http://docs.jquery.com/Plugins/livequery

"Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated.

For example you could use to bind a click event to all A tags, even any A tags you might add via AJAX."


On Thu, Oct 2, 2008 at 15:19, light-blue <Sean.Wingert@gmail.com> wrote:

Hi Leonardo,

I was thinking about that too, but I need to rebind that event handler
after an Ajax call. The following works, but seems ugly. Any pointers?

$(document).ready(function() {
function bindBuilding() {
      $('a.edit-building').click(function(){
       var building = function (data) {
         //do stuff
     }
    $.get(this, null, building);
     return false;
               });
       }

bindBuilding();

function doAjaxStuff() {
   //perform ajax stuff

   bindBuilding();
}


});






On Oct 2, 11:03 am, "Leonardo K" <leo...@gmail.com> wrote:
> Why don't use the simple click function?
>
> $('a.edit-building').click(function(){
>     //do stuff
>      return false;
>
> });
> On Thu, Oct 2, 2008 at 14:43, light-blue <Sean.Wing...@gmail.com> wrote:
>
> > Hopefully simple problem. I click an img link, html like this
>
> > <a class="edit-building" href="/edit-building/145">
> > <img src="icons/edit.png"/>
> > </a>
>
> > jquery like this
>
> > if ($(event.target).is('a.edit-building')) {
> >  var building = function (data) {
> >  ..do stuff
> >  }
> >  $.get(event.target, null, building);
> >  return false;
> > }
>
> > but the binding doesn't work, probably because the target is my img
> > not my link. How can I click the image and fire the code for my a.edit-
> > building?
>
> > Thanks!

No comments: