I have been making a form in html that has a checkbox and if checked it displays another checkbox using Javascript. I have now changed that form to use django’s Form library but I don’t know how to access the display attribute of the django input fields. I have tried the HideInput widget but all that doesn’t hide the label just the box itself.
Any help on how to set the display attributes of django Form objects and/or how to toggle those attributes using Javascript would be great and/or how to code event attributes on django Forms would be great!
models.py:
class ContactForm(forms.Form):
Contact = forms.BooleanField(required=False, widget=CheckboxInput())
More = forms.BooleanField(required=False, widget=CheckboxInput())
html page:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function(){
$('#id_Contact').click(function(){
if($('#id_Contact').is(':checked')){
$('#id_More').show();
}
});
});
function ifChecked(id, id2){
var ele = document.getElementById(id);
var ele2 = document.getElementById(id2);
if(ele.checked){
ele2.style.display = "block";
}
else{
ele2.style.display = "none";
}
}
</script>
<form action="/contact/" method="post">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
You can do this.
You could use jQuery