0

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.

1
  • change $('a#test') to $('button#test') or better $('#test') Commented Sep 23, 2020 at 13:58

1 Answer 1

1

@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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this solved the issue. I am not familiar with JavaScript :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.