1

I know how to generate only one the button. I'm doing it this way:

var mylist = ['first', 'second', 'third', 'fourth', 'fifth']
var rand = Math.floor(Math.random() * mylist.length)
var word = mylist[rand]

var btn = document.createElement('input')
btn.id = 'b1'
btn.value = word
btn.type = 'button'
document.body.appendChild(btn)

But I need to generate a few buttons(for example 3 buttons). document.body.appendChild(btn)

5
  • 1
    Use a for loop, for example: stackoverflow.com/a/13722547/1161948 Commented Dec 7, 2016 at 2:35
  • 4
    Learn the language basics, like arrays and loops; this is trivial. After you've mastered the simple stuff, then you'll have a problem when you attach onClick listeners to them that will cause the last button to activate no matter which one you press--that's a real problem, and we'll show you how to fix that when you get there. Commented Dec 7, 2016 at 2:35
  • You should use btn.setAttribute('id', 'b1') and btn.innerHTML = word Commented Dec 7, 2016 at 2:39
  • @Matías Both aren't ideal suggestions. Why use setAttribute when id is available for free? It's also not wise to insert potentially random HMTL into a button when a value is all that's required. Commented Dec 7, 2016 at 2:54
  • const input = document.createElement('input') then input.value = 'foo' will create an empty input tag because you can't set the value of a button with value Commented Dec 7, 2016 at 3:05

3 Answers 3

3

Just wrap your logic in a for-loop .

Example :

var mylist = ['first', 'second', 'third', 'fourth', 'fifth']
var rand = null; 
var word = null;
var threshold = 3


    for(var i = 0 ; i < threshold ; i++){

        rand = Math.floor(Math.random() * (mylist.length - 1)) + 1;
        word = mylist[rand];
        var btn = document.createElement('input');
        btn.id = 'b' + i;
        btn.value = word;
        btn.type = 'button';
        document.body.appendChild(btn);

    }

Updated with proof :

http://codepen.io/theConstructor/pen/WoJEGy?editors=1010

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

This code won't work as is, because of word being undefined.
Updated to reflect
in your code example you have , var mylist = ['first', 'second', 'third', 'fourth', 'fifth'] ...that should be mylist
I think you're getting me confused with OP. It helps to provide working code examples when the situation is as straight forward as this.
1

Use a for-loop in conjunction with the <button> element (rather than <input type="button">).

var list = ['first', 'second', 'third', 'fourth', 'fifth']

for (var i = 0; i < 100; i++) {
    var btn = document.createElement('button')
    var random = Math.floor(Math.random() * list.length)
    btn.textContent = list[random]
    document.body.appendChild(btn)
}

Comments

1

Most performant solution as you're only appending the buttons to the DOM just once by using a document fragment.

const mylist = ['first', 'second', 'third', 'fourth', 'fifth']
const frag = document.createDocumentFragment();

for (let i = 0, listLen = mylist.length; i < listLen; i++) {
  let button = document.createElement('input');
  button.type = 'button'
  button.id = `b${i + 1}`;
  button.value = mylist[Math.floor(Math.random() * listLen)]

  frag.appendChild(button);
}

document.body.appendChild(frag);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.