Q:

Uncaught TypeError: Cannot read property 'nodeType' of null

I'm trying to do a knockout.js project for school and I need to bind an observable array to a select dropdown. The following code does not work and gives this error on the applyBindings line:

Uncaught TypeError: Cannot read property 'nodeType' of null

Any ideas? This is my code:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="knockout.js"></script>
    <script>
        function ViewModel() {
            var self = this;
            self.options1 = ko.observableArray([{id: 0, name: "Example option"}]);
        }
        ko.applyBindings(new ViewModel());
    </script>
  </head>
  <body>
    <!-- page content -->
    <select data-bind="options: options1"/>
  </body>
</html>

A:

You should not call ko.applyBindings until you're sure the DOM's loaded. Currently, in your code, you execute applyBindings right when that line of code hits. At that point, the body tag hasn't even been encountered yet by the browser, so it won't know what to bind to.
A quick fix would be:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="knockout.js"></script>
  </head>
  <body>
    <select data-bind="options: options1"/>
    <!-- page content -->
    <script>
        function ViewModel() {
            var self = this;
            self.options1 = ko.observableArray([{id: 0, name: "Example option"}]);
        }
        ko.applyBindings(new ViewModel());
    </script>
  </body>
</html>

Alternatively, you could defer said code until the DOM's loaded. If you're also using jQuery in your project you could use the $(document).ready idiom for that.

