0

I am trying to load a variable into a HTML page when pressing a button. The problem is that I already have a function to load the initial page, and the new data shall be loaded on the same page, without redirecting. This is the code i have by now:

.py file: 

@app.route('/')
    def index():
       return render_template('index.html')

def get_documents():
   documents = {'asd': 'as', 'def':'de'}
   return render_template('index.html', documents=json.dumps(documents))

index.html file

<div>
  <button onclick="Start()" id="StartButton" >Start</button>
</div>


<div id = "feed" style="display: none;">  
</div>
<script>
    var d = JSON.parse('{{documents | tojson | safe}}');
    document.getElementById('feed').innerHTML = d.asd; 
 </script>

.js file:

function Start() 
{
  var feed_div = document.getElementById("feed");
  feed_div.style.display = "block";
}

I would like that when i press the Start button, the content in the documents structure would appear in the .feed section. How could i do this?

Also, documents will be a list, but here i used a json because i wanted to try some examples i've found.

1 Answer 1

1

You can create div element inside the feed element, this element will contain all of your document data and will be hidden

<div id="document" style="display: hidden">
    {% for item in documents %}
        <div>{{ item }}</div>
    {% endfor %}
</div>

and then in javascript change the display of the document element:

function start() {
    document.getElementById("document").style.display = "block";
}
Sign up to request clarification or add additional context in comments.

Comments

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.