Flask: multiple parameters for onClick function

Viewed 36

I am trying to pass multiple values, note.id and attractionName into javascript via an onclick function.

html button

<button type="button" class="close" onClick="deleteNote({{ note.id}}, {{attractionName}})">

javascript

 function deleteNote(noteId, attractionName) {
    fetch("/delete-note", {
      method: "POST",
      body: JSON.stringify({ noteId: noteId, attractionName }),
    }).then((_res) => {
      window.location.href = "/attraction/" + attractionName;
    });
  }
  

views.py

@views.route('/delete-note', methods=['POST'])
def delete_note():
    flash("delete note")
    note = json.loads(request.data)
    noteId = note['noteId']
    flash(noteId)
    note = Note.query.get(noteId)
    if note:
        if note.user_id == current_user.id:
            db.session.delete(note)
            db.session.commit()

    return jsonify({})
1 Answers

if attractionName is a string, you need to enclose in quotes, such as:

<button type="button" class="close" onClick="deleteNote({{ note.id}}, '{{attractionName}}')">
Related