If I have 2 inputs, and in each of them I will write a name, how can I generate a random name from 2 input with javascript code by pressing a button?

  • Bring more clarity into your question, preferably with an example. How should a name be generated? What did you do? - Sergey Glazirin
  • сгенерировать случайное имя so it’s chtoli - Vasya, Petya = Pesya? - Artem Gorlachev
  • Yes, using only JS - Asking

1 answer 1

 $("#btn").click(function() { var temp = Math.floor(Math.random() * 2); var randName = $("#inp" + temp).val(); alert(randName); }); 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <input type="text" id="inp0" /> <input type="text" id="inp1" /> <input type="button" id="btn" value="Click me!!!" /> </body> </html> 

  • How will it be without jquery? - Asking
  • var btn = document.getelementbyid("btn");btn.onclick = function(){ var temp = Math.floor(Math.random() * 2);var inp = document.getelementbyid("inp"+temp);alert(inp.value);}; Wrote from the phone ... there may be blots - Vitaly Shebanits