I have following jQuery which modifies my html code:
$('input[name="date-format"]').change(function () {
if ($(this).val() == '0') {
$(".date-range").html(`<div class="eu-input">
<label>Date from</label><input name="eu-date-from">
<label>Date to</label><input name="eu-date-to">
</div>`);
}
else if ($(this).val() == '1') {
$(".date-range").html(`<div class="us-input">
<label>Date from</label><input name="us-date-from">
<label>Date to</label><input name="us-date-to">
</div>`);
}
});
On the backend however, I have python flask and I am trying to pass flask-wtf form within the modified html. Plain html and python code on the backend would look like this:
<div class="eu-input">
<label>Date from</label>
{{ form.eu_dates() }}
<label>Date to</label>
{{ form.eu_dates() }}
</div>
@app.route('/', methods=["GET", "POST"])
def home():
form = MyForm()
return render_template('index.html', form=form)
I am struggling to pass the python object into the JavaScript code and then include within the new html content. Thanks for your help.
P/S: I wouldn't wish to use .show()/.hide() method to achieve my goals because the forms are required and despite that they are hidden they still exist in the html code. This is my main reason for replacing the entire html code inside the div.