views:

15

answers:

1

When using a formset, you need to add {{formset.management_form}} to your form. This renders 3 fields, one of which is TOTAL_FORM_COUNT. I want to get the ID of this field so that I can modify its value via JS. How can I do that?

I'm trying stuff like formset.management_form.TOTAL_FORM_COUNT and .fields.TOTAL_FORM_COUNT to get the field, and then I think auto_id should hold the attribute I need, but I can't figure out how to access it.

A: 

It should be possible to get the value of the total_form_count from the field.

When I needed the same however, I got it from within jQuery:

$('id_your-model-name_set-TOTAL_FORMS').val()

Since I guess you are going to be replacing the value because you manually add more forms via javascript, you are going to have to select the element, to increment/set it. You might as well get it's defined value there.

Lakshman Prasad
You forgot the `#`. I know how to retrieve the value with jQuery, but I wanted a more generic solution in case the ID changes. i.e., I want to do `$('#{{formset.total_forms.id}}').val()` instead.
Mark