Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
This is my javascript:

<script src="../Scripts/knockout-3.0.0.js"></script>
<script type="text/javascript">
    alert("hiii");
        function AppViewModel() {
        this.firstName = ko.observable("Amol");
        this.lastName = ko.observable("Padher");

        this.fullName = ko.computed(function () {
            return this.firstName() + " " + this.lastName();
        }, this);

        this.capitalizeLastName = function () {
            var currentVal = this.lastName(); // Read the current value
            this.lastName(currentVal.toUpperCase()); // Write back a modified value
        };
    }

    // Activates knockout.js
    ko.applyBindings(new AppViewModel());
</script>


XML
Below the html code:

<body>
    <form id="form1" runat="server">
        <div>
        <!-- This is a *view* - HTML markup that defines the appearance of    your UI -->

            <p>First name: <strong data-bind="text: firstName"></strong></p>
            <p>Last name: <strong data-bind="text: lastName"></strong></p>

            <p>First name: <input data-bind="value: firstName" /></p>
            <p>Last name: <input data-bind="value: lastName" /></p>

            <p>Full name: <strong data-bind="text: fullName"></strong></p>

            <button data-bind="click: capitalizeLastName">Go caps</button>
        </div>
    </form>
</body>
How to run the above code using Knockout? I.e. how to call the function AppViewmodel() in this program? please sugguest me any solution!!
Posted
Updated 4-Aug-15 19:44pm
v2
Comments
Sreekanth Mothukuru 4-Aug-15 2:21am    
Does it throw any error or logs message in debug/console ?
[no name] 5-Aug-15 1:44am    
yes. error is- ncaught TypeError: Cannot read property 'nodeType' of null(error on console)
[no name] 5-Aug-15 1:35am    
yes. error is- ncaught TypeError: Cannot read property 'nodeType' of null

1 solution

i got the solution. and it is-


C#
$(document).ready(function (){
    // Activates knockout.js
    ko.applyBindings(new AppViewModel());
})
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900