0

At the end of running this script, I should see the originally empty div id="checkboxes" becomes:

<div id="checkboxes">
      <label><input type="checkbox">option1</label>
      <label><input type="checkbox">option2</label>
      <label><input type="checkbox">option3</label>
</div>

How I tried to obtain this was (because I have the exact options in an array):

<script type="text/javascript">

    function populate() {
        debugger;
        var x = document.getElementById("checkboxes");
        var optionArray = ["option1", "option2", "option2"];


        for (var option of optionArray) {
            var label = document.createElement("label");
            label.innerHTML = "<input type='checkbox'>" + option;
            x.appendChild = label;
        }
    }

    populate();


</script>

<html>
<head>
        <div id="checkboxes">
        </div>

</head>
</html>

I get an error like

Uncaught TypeError: Cannot set property 'appendChild' of null

at the line where I try to append the child, can someone advise why please

Sami
  • 353
  • 2
  • 16
  • 1
    Why do you have `
    `s in your ``? Where is your `
    – Sebastian Simon Nov 09 '17 at 09:22

1 Answers1

0
//x.appendChild = label;
x.appendChild(label);
kyun
  • 7,487
  • 4
  • 21
  • 44