create random buttons

-4

I need to create 3 buttons in javascript in such a way that when the page is reloaded, each button is assigned a number from 1 to 3 so that later those buttons call another function. I do not hear anything. Just make a button randomly have a number from 1 to 3 but when they are 3 and do not repeat the plan that the button 1 if you have 1 do not have the 2 or 3 and so with the other buttons . If anyone knows how I could do it or explain a bit the logic that must be followed because the truth is very grated

    
asked by notengologica 09.12.2017 в 01:20
source

2 answers

0

I hope this is what you are looking for

$(".btn").click(function(event) {
  function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;


  while (0 !== currentIndex) {

    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}
var arr = [1,2,3];
arr = shuffle(arr);

  $(".con-botones").html('<button type="button" name="button">'+arr[0]+'</button><button type="button" name="button">'+arr[1]+'</button><button type="button" name="button">'+arr[2]+'</button>') 
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn" type="button" name="button">Botones Aleatorios</button>

<div class="con-botones">
      
</div>
    
answered by 09.12.2017 в 02:32
-3

You need to identify each button, for example to the button one you put id="1" to two id="2" to three id="3". Now that you have identified the buttons from javascript, you assign them the value $ ("# 1"). Value ("the value");

I hope I have helped you a greeting

    
answered by 09.12.2017 в 01:33