How to set flaskform field value by javascript

Viewed 182

I have a JavaScript calender that when a user clicks on a date, the date is submitted together with other fields when the User submits the FlaskForm for validation.

I've managed to obtain the date dynamically, but when I set the date as the FlaskForm field value, the form validates but the date field comes out empty.

How can I properly submit the dynamic date data to FlaskForm using JavaScript?

Python

if createslot_form.validate_on_submit():
  slot = Slot(
    start_time=str(createslot_form.start_time.data), 
    end_time=str(createslot_form.end_time.data),
    quantity=int(createslot_form.quantity.data), 
    date=str(createslot_form.date.data),
    room_id=int(room_id)
  )
  db.session.add(slot)
  db.session.commit()
  print('slot has been added')
  return redirect(url_for('edit_room', room_id=room_id))
return render_template('room.html', room=room, createslot_form=createslot_form, created_slots_json=created_slots_json, room_link=str(room_link))

Javascript

function new_event(event) {
  if ($(".active-date").length != 0) {
    var day = $(".active-date")[0].innerText;
    var month = $(".active-month")[0].innerText;
    var year = $(".year")[0].innerText;
    var click_date = day + ' ' + month + ' ' + year;
    console.log($(".form-control")[0]);
    
    $(".form-control")[0].value = click_date;
    $(".form-control")[0].placeholder = click_date;
  }
}
0 Answers
Related