Q:

How to add a selected number of TD's with JavaScript?

I am trying to make a function that adds the selected number from the <select> in td's to the tr with id="spelers". But when I run it it doesn't work. Could someone tell me what I am doing wrong? Thanks in advance.
Here's the HTML:
<select id="speler_select" onchange="spelers()">
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>

<table>
    <tr id="spelers"></tr>
</table>

And this is the JavaScript:
// number of players
var speler_select = document.getElementById("speler_select");
var aantal_spelers = speler_select.options[speler_select.selectedIndex].value;

var spelers = document.getElementById("spelers");

function spelers() {

    for (var x = 0; x < aantal_spelers; x++) {
        var td = document.createElement("td");
        spelers.appendChild(td);
        spelers.lastChild.innerHTML = "Speler " + (x + 1);
    }
}

A:

You need to get the selected value inside the spelers() function. If you get the value outside of the spelers() function you'll get the initial value, not the actualy selected one.
Another thing: you need to convert the selected value from string to integer to be able to iterate on it.
Another thing: spelers variable and spelers function have the same name, therefore when defining spelers function it replaces spelers variable. You should use different names.
var speler_select = document.getElementById("speler_select");
var spelers_e = document.getElementById("spelers");
function spelers() {
    var aantal_spelers = parseInt(speler_select.options[speler_select.selectedIndex].value, 10);
    for (var x = 0; x < aantal_spelers; x++) {
        var td = document.createElement("td");
        spelers_e.appendChild(td);
        spelers_e.lastChild.innerHTML = "Speler " + (x + 1);
    }
}

Also it would be a good idea to add first option with no value, so a user can actually select the first option with a value to run the spelers() function. Like this:
<select ...>
  <option value=''>Add spelers</option>
  <!-- other options here -->
</select>

