Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
How to pass the variable in python to the JavaScript

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're using a random number generator and 0 (zero) may be a valid output of your generator sometimes so it can difficult to debug the problem. Also, apparently you're missing a couple of {{}} in your <input>.

<input value={{value}} type="number" id="myVariable"/>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda