Button does not run Javascript

Asked

Viewed 188 times

0

I’m having trouble making the button responsible for generating the alert with the draw number start javascript, I’m using the code below for javascript:

    <script type="text/javascript">


    $('#btnGnrt').click(function() {

    function sorteio_numero(nummin, nummax, numqtd){
        var amostra   = new Array(),
        lista     = new Array(),
        tamanho   = 0,
        aleatorio = 0;
        for(var i = nummin; i <= nummax; i++){
            amostra.push(i);
        }
        while( lista.length < numqtd ){

            tamanho = amostra.length;


            aleatorio = Math.floor( Math.random() * tamanho );


            lista.push( amostra.splice(aleatorio,1) ); 
        }
        return lista;
    }

    alert(sorteio_numero(1000,9999,1));

</script>

and the next to the button:

<div class="button">
    <br><button id="btnGnrt">Gerar Sorteio</button><br>
</div>

The page contains only the button and a header with the title, I can’t see the reason why the button "does not work"

  • in place of aleatorio, would not be resultado?

  • 1

    Probably has to do with the scope, try to put $('#btnGnrt').click(function() {} Within a $(document).ready(function(){});

  • Yes, I had changed the "randomness" for "result" but I missed one, I corrected the question

  • I wouldn’t have any problem with you calling function sorteio_numero no onclick inline button, but... goes to taste

  • I put inside the $(Document). ready(Function(){}); and did not solve, I think I will try to call in onclick same

2 answers

2

Missing close the structure of the click, that I commented below:

$('#btnGnrt').click(function() {
    function sorteio_numero(nummin, nummax, numqtd) {
        var amostra = new Array(),
            lista = new Array(),
            tamanho = 0,
            aleatorio = 0;
        for (var i = nummin; i <= nummax; i++) {
            amostra.push(i);
        }
        while (lista.length < numqtd) {
            tamanho = amostra.length;
            aleatorio = Math.floor(Math.random() * tamanho);
            lista.push(amostra.splice(aleatorio, 1));
        }
        return lista;
    }

    alert(sorteio_numero(1000, 9999, 1));
});  // essa estrutura aqui 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="button">
    <br><button id="btnGnrt">Gerar Sorteio</button><br>
</div>

2


There are several errors in your code:

  1. You didn’t properly close that block $('#btnGnrt').click(function() {

  2. tamanho = 0,should end a line with ;

  3. tamanho = amostra.length;there’s no need for that to be inside the while being assigned to each interaction.

Obs: I modified your code to js pure.

Working

var button = document.querySelector('button');
button.addEventListener('click', function () {
    console.log(sorteio_numero(1000, 9999, 1));
})

function sorteio_numero(nummin, nummax, numqtd) {
    let amostra = [];
    let lista = [];
    let tamanho = 0;
    let aleatorio = 0;
    
    //Adiciona todos os números entre um Rang
    for (let i = nummin; i <= nummax; i++) {
        amostra.push(i);
    }
    
    tamanho = amostra.length;
    
    while (lista.length < numqtd) {
      aleatorio = Math.floor(Math.random() * tamanho);
      lista.push(amostra.splice(aleatorio, 1));
    }
    return lista;
}
<div class="button">
    <br><button id="btnGnrt">Gerar Sorteio</button><br>
</div>

  • Dude, var within function has no global scope no.

  • @Sam yes, I’ve read about here, I thought I had. I cheated

Browser other questions tagged

You are not signed in. Login or sign up in order to post.