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

356
Visualizações
Embedding scripts in html buttons

So, I'm a beginner programmer and figured I could do something short and fun. I made a short program in like 5 minutes, but I ran into a problem where I can't figure out, how to make a button that runs a said script when pressed (e.g. makes an alert with a random number in it) Here's my code

<!DOCTYPE html>
<html>
    <Head>
        <h2>
        Random number Generator
        </h2>
    </Head>
    <body>
         <script src ="Script.js"></script>
        <button type="button" onclick="alert(randomNum)"> Generate! </button>
    </body>
</html>

and the script part here

function randomNum(result, oneToHundred, randomNum) {
    let newNumber = Math.random()
    let oneToHundred = newNumber * 1000
    let result = Math.floor(oneToHundred)
    return result
};
alert(randomNum)

I just can't figure out how to make the button search for a script and run it when pressed. I tried to embed the script search to the onclick part, but i got a syntax error.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This would be simple example.

HTML file.

<!DOCTYPE html>
<html>
    <Head>
        <h2>
        Random number Generator
        </h2>
    </Head>
    <body>
         <script src ="Script.js"></script>
        <button type="button" onclick="showMessage()"> Generate! </button>
    </body>
</html>

JavaScript file.

function showMessage() {
    alert("Hello friends, this is random number: " + Math.floor(Math.random() * 11));
}
about 4 years ago · Santiago Trujillo 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