-1

So this is my code. I want to create buttons like

Random 1 Random 2 .... ... Random 10

Is there any way I can reach it with for loop in JavaScript? And so is id, like the first button's id is "button1" ....

<script type="text/javascript">
<!--
  var i = 0;
  var x = new Array(11);
  var y = new Array(11);
  for (i=1; i<=10; i++)
  {
  x[i] = 35.38825899+Math.random()*0.007962;
  y[i] = 0.03903201/0.007962*x;
  document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');
  }
//-->
</script>
pikkuu
  • 71
  • 2
  • 11

4 Answers4

0
document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');

to

document.write('<input type="button" id="button' + i + 'value="Random' + i + '" onclick="sfcshonan()"/>'+'<br />');


Or with jQuery
var button = $("<input>").attr("type", "button").attr("id", "button" + i).val("Random" + i).click(sfcshonan)

$(document).append(button)
Mohayemin
  • 3,841
  • 4
  • 25
  • 54
0

Try this

   document.write('<input type="button" id="button'+i+'" value="Random'+i+'" onclick="sfcshonan()"/>'+'<br />');

Insted of

   document.write('<input type="button" value="Random" onclick="sfcshonan()"/>'+'<br />');
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
0
document.write('<input type="button" value="Random '+i+'" onclick="sfcshonan()"/>'+'<br />');
Fawad Ghafoor
  • 6,039
  • 7
  • 41
  • 53
0

Simple. Change the:

value="Random"

To:

value="Random ' + i + '"

Final Code

document.write('<input type="button" id="button'+i+'" value="Random'+i+'" onclick="sfcshonan()"/>'+'<br />');
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252