sacabuche
sacabuche

Reputation: 2849

How to bind events with jQuery in django admin when new *Inline is added

I made a jQuery function that binds some fields, but it doesn't work when I add a new inline.

The main problem is that I don't know how to detect the insertion in DOM and bind it to my function.

Upvotes: 6

Views: 2806

Answers (4)

Kilian Perdomo Curbelo
Kilian Perdomo Curbelo

Reputation: 1358

I know this is super old, but just in case anybody else is wasting hours trying to wrap their heads around this (like I'm doing now), here is how to do it: listen to the formset:added event and you've got it:

$(document).bind('formset:added', function(e) {
    console.log("New inline formset added: ", e.target)
});

Upvotes: 0

metamemetics
metamemetics

Reputation: 675

$('.add-row a').click(my_function)

as long as my_function does not include return false; it shouldn't interfere with the existing functionality of that button

Upvotes: 0

Roel Kramer
Roel Kramer

Reputation: 391

The suggestions above do not work for this particular problem. I'll file a bug and ask if they can assign an ID to that link button, or fix it another way.

Edit: bug filed https://code.djangoproject.com/ticket/16359

Upvotes: 0

Anurag
Anurag

Reputation: 141909

Use the live or delegate functions to bind to form elements. They will continue working even after you insert new form elements, and you don't need to re-attach the event handlers every time.

Related questions:

Upvotes: 2

Related Questions