I am trying to create a collapsible form in Django which would toggle a form once a button is clicked. I found that I could use the .toggle() option from jQuery but I don't know how to connect the associated form with the function
The "toggles all paragraphs" example from http://api.jquery.com/toggle/ seems to be what I need but I'm not sure how to integrate it.
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$('#toggle').click(function() {
$('form').toggle('slow');
});
</script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<title>Hello world!</title>
</head>
<body>
<h3 class="text-success">Add Sensor</h3>
<br>
<form style="display:none;" method="post">
{% csrf_token %}
<div class="row align-items-center">
<div class="col-sm-8">
<table>
{{ form1.as_table}}
</table>
<div class="mx-sm-2">
<input type="submit" value="Submit">
</div>
<br>
<br>
<button type="button" id="toggle">toggle form</button>
<h3 class = "text-success">Add Sensor View</h3>
<table>
{{ form2.as_table}}
</table>
<div class="mx-sm-2">
<input type="submit" value="Submit">
</div>
<br>
<br>
</div>
<div>
</form>
I would like form2 to be toggled only once a button is pressed
Both Forms are defined similar to the following:
class VehicleForm(forms.ModelForm):
class Meta:
model = Vehicle
fields = ['vehicle_id',
'vehicle_name',
'vehicle_modelyear',
'vehicle_version',
'vehicle_file']
widgets = {
'vehicle_id': forms.Textarea(attrs={'rows': 1, 'cols': 15}),
}