How to get python variable to the JavaScript?
First, I make the random variable in python then pass it to the index.html
@app.route('/index', methods=['GET','POST'])
def index():
value = random.randrange(8)
return render_template('index.html', value = value)
Then, I get the value in html and get the value by input
<div class="print">{{value}} is passed.</div>
<input value=value type="number" id="myVariable"/>
Next, I get the variable in html to the JavaScript
<script type='text/javascript'>
alert(document.getElementById('myVariable').value)
I really want to get the variable from the python to show the random pictures in html using JavaScript
document.write("<img src='" + images[myVariable] + "'/></a><p><span>"
+ caption[myVariable] + "</span></p>");
This is the final one what I want to do using this source code. But it doesn't work and index is always 0. How should I solve it?