Skip to content Skip to sidebar Skip to footer

How To Create Buttons With Different Value Using For Loop

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 but

Solution 1:

Simple. Change the:

value="Random"

To:

value="Random ' + i + '"

Final Code

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

Solution 2:

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

Solution 3:

document.write('<inputtype="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)

Solution 4:

Try this

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

Insted of

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

Post a Comment for "How To Create Buttons With Different Value Using For Loop"