Label not some Django

I use a radio and a JavaScript script to do the action of disappearing the agency field of the Model report, the problem is that add only the combobox the label does not add.

Model

class Relatorio(models.Model):
    data_inicial = models.DateField(default=timezone.now)
    data_final = models.DateField(default=timezone.now)
    tipo = models.CharField(max_length=30, choices = TIPOS, default='ENVIO')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE)
    agencia = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True)

JavaScript

<script>
jQuery(document).ready(function($) {
    $('input[type="radio"]').change(function(){    
        if ($(this).is('#id_tipo_1')){
             $('#id_agencia, label[for="#id_agencia"]').parent().hide()     

         }else{
             $('#id_agencia, [for="#id_agencia"]').parent().show()

         }
   });
});
</script>

Demo:

insert the description of the image here

insert the description of the image here

Author: Sam, 2019-03-28

1 answers

Your mistake is that you are putting the symbol # in the label selector, and this way the selector does not find the element:

[for="#id_agencia"]
      ↑

When it should only be:

[for="id_agencia"]

Also does not need to include label in the selector, just like this:

jQuery(document).ready(function($) {
   $('input[type="radio"]').change(function(){    
      if ($(this).is('#id_tipo_1')){
         $('#id_agencia, [for="id_agencia"]').parent().hide()     
      }else{
         $('#id_agencia, [for="id_agencia"]').parent().show()
      }
   });
});

An example:

jQuery(document).ready(function($) {
   $('input[type="radio"]').change(function(){    
      if ($(this).is('#id_tipo_1')){
         $('#id_agencia, [for="id_agencia"]').parent().hide()     
      }else{
         $('#id_agencia, [for="id_agencia"]').parent().show()
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="rad" id="id_tipo_2" checked>Envio
<input type="radio" name="rad" id="id_tipo_1">Recebimento
<br>
<table>
   <tr>
      <td>
         <label for="id_agencia">Agência</label>
      </td>
      <td>
         <select id="id_agencia">
            <option>------</option>
         </select>
      </td>
   </tr>
</table>
 1
Author: Sam, 2019-03-28 17:28:14