JavaScript runtime error: Unable to get property 'nodeType' of undefined or null reference

14,240

You called applyBindings before browser rendered html. You have to move script tag to the bottom of the page or put your code to document ready handler:

<script type="text/javascript"> 
$(function(){ 
    debugger;
    function viewModel() {
        this.day = ko.observable('24');
        this.month = ko.observable('02');
        this.year = ko.observable('2012');

        this.fullDate = ko.computed(function () {
            return this.day() + "/" + this.month() + "/" + this.year();
        }, this);
    };

    ko.applyBindings(new viewModel());
});
</script>
Share:
14,240
Admin
Author by

Admin

Updated on September 06, 2022

Comments

  • Admin
    Admin almost 2 years

    I tried running an application with knockoutjs script included with jquery and my own js file that has a ViewModel actual binding to the controls.

    I am getting the exception every time i run the application.

    Is this the issue in my system, or the Visual Studio? I even tried running the html file alone in the browser, i couldn't see any exceptions but it stopped me from performing all other functionalists that are expected to be done from knockoutjs.

    Please help me in this regard

    This is my full code

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="Scripts/jquery-1.7.1.min.js"></script>
        <script src="Scripts/knockout-2.2.0.js"></script>
    
        <script type="text/javascript">        // Here's my data model
            debugger;
            function viewModel() {
                this.day = ko.observable('24');
                this.month = ko.observable('02');
                this.year = ko.observable('2012');
    
                this.fullDate = ko.computed(function () {
                    return this.day() + "/" + this.month() + "/" + this.year();
                }, this);
            };
    
            ko.applyBindings(new viewModel());
    
        </script>
    
    </head>
    <body>
        <p>Day:
            <input data-bind="value: day" /></p>
        <p>Month:
            <input data-bind="value: month" /></p>
        <p>Year:
            <input data-bind="value: year" /></p>
        <p>The current date is <span data-bind="text: fullDate"></span></p>
    
    
    </body>
    </html>
    
    • collapsar
      collapsar almost 11 years
      where do you define ko ?
    • Admin
      Admin almost 11 years
      See this line : ko.applyBindings(new viewModel()); and see the script file