Search code examples
jquerydjangohidden-fieldinline-formset

How to exclude "hidden" type fields while updating the form field values using jQuery


In my Django application, I am trying to update input fields using jQuery. The fields being updated are formset fields and one of the fields is related to a parent model for enforcing foreign key relationship.

As is designed in Django, the foreign key fields are also cloned during dynamic formsets addition and have type "hidden" as against other visible fields as defined (in models/form widgets).

This is what I am doing to update a value in a text type field (during form addition):

$('#'+tempNumTag + ' input').val(tempNum);

where:

  1. tempNumTag : Hash tag constructed for each row of formset
  2. tempNum: Value being updated in the field (with id "tempNumTag")

Doing so my text type inputs get updated with the value ("tempNum") but the "hidden" field's "value" also gets updated at the same time, with the same value as that of the variable "tempNum".

What I would like to do instead is:

//  if field type is not hidden {
// Something on the line: "input:hidden"
        $('#'+tempNumTag + ' input').val(tempNum);
//  }

so that the hidden field is excluded from the above update.

How may I do a conditional update excluding hidden field?


Solution

  • The css selector for hidden elements is :hidden, the one for excluding hidden elements is :not(:hidden) so input:not(:hidden) will select all input elements that aren't hidden:

    $('#'+tempNumTag + ' input:not(:hidden)').val(tempNum);