jeudi 15 octobre 2020

How random options in button?

 var options = [$("#option1"), $("#option2"), $("#option3"), $("#option4")]
            let randomOption = options.sort(() => .5 - Math.random());;
            console.log(randomOption[0])
            console.log(randomOption[1])
            console.log(randomOption[2])
            console.log(randomOption[3])
            $("#option1").text(randomOption[0].text())
            $("#option2").text(randomOption[1].text())
            $("#option3").text(randomOption[2].text())
            $("#option4").text(randomOption[3].text())
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<button id="option1">1</button>
<button id="option2">2</button>
<button id="option3">3</button>
<button id="option4">4</button>

Why random doesn't work ?

cosole.log is correct, but in fact not real. I want to move random option in button.

But if I reload the page many times only $("#option1").text() is correct.




Aucun commentaire:

Enregistrer un commentaire