I need to send string from HTML file to Python(Flask) using JavaScript.
This is my HTML code inside “templates” folder:
<button type="button" class="btn btn-primary btn-lg" id="test">Large button</button>
<script type="text/javascript"
src="{{ url_for('static', filename='index.js') }}"></script>
This is my JavaScript code (index.js) inside “static” folder
$(function() {
$('a#test').bind('click', function() {
//var value = document.getElementById("msg").value
$.getJSON('/run',
//{val:value},
function(data) {
// do nothing
});
return false;
});
});
This is the code for my main.py
from flask import *
#some code
@app.route("/run")
def run():
print("clicked")
return "none"
It is supposed to work, but the button doesn’t work, “clicked” isn’t being printed when the button is being pressed.
Advertisement
Answer
@geatanoM change $('a#test') to $('#test') as there should only ever be one element with test as its id and your trying to call an a tag in your code rather than a button