Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Issue using flask variable on Onclick function in Javascript

Geek07

New Coder
First of all, I don't any previous experience with Javascript, this is my first webapp.

So I'm creating an webapp to construct an URL based on user input and query result, send it to frontend and I'm trying to open it in a new tab on browser.
I'm using flask to handle with server side and Javascript to open the URL on browser. My main difficult until now it's how to handle with flask variable, I can send it to the html perfectly but I can not set it before on the first 'onclick event', on first execution the flask variable shows empty and it's only setted when the 'onclick=open_new_tab()' ends. Note that flask variable depends on the user input.

Anyone could help me?

index.html:
HTML:
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
  <title>Search by NIF</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='main.css')}}">
</head>
<body>
  <form action= 'home' method="post">
    <script type="text/javascript">
      function success() {
            if(document.getElementById("nif").value == 0 || document.getElementById("nif").value===0) {
            document.getElementById('button').disabled = true;
        } else {
            document.getElementById('button').disabled = false;
        }
      }
      function open_new_tab() {
        var data = '{{data}}';
        var url = data.replace(/&amp;/g, '&');
        return window.open(url, '_blank').focus();
      }
    </script>
    {% for message in get_flashed_messages()%}
      <p>{{message}} </p>
    {% endfor %}
    <br>
    <input class='input' oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" id="nif" aria-label= "NIF" type="number" min="1" maxlength="9" onkeyup="success()" onkeydown="return event.keyCode !== 69" name="nif_input" required>
    <br>
    <button id='button' onclick="open_new_tab()" disabled>Search</button>
  </form>
</body>
</html>
 

New Threads

Buy us a coffee!

Back
Top Bottom