Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:44:51+00:00 2026-06-14T15:44:51+00:00

I put my code into this jsFiddle here: http://jsfiddle.net/damianofusco/ngFM9/ function Person(data) { this.Id =

  • 0

I put my code into this jsFiddle here:
http://jsfiddle.net/damianofusco/ngFM9/

function Person(data) {
    this.Id = data.Id;
    this.FirstName = ko.observable(data.FirstName);
    this.LastName = ko.observable(data.LastName);
    this.FriendlyName = ko.computed(function() {
        return this.Id + ' ' + this.FirstName() + ' ' + this.LastName();
    }, this);
    this.HasValues = ko.computed(function() {
        return this.FirstName().length > 0 && this.LastName().length > 0;
    }, this);
}

var pBlank = new Person({
    Id: 0,
    FirstName: "",
    LastName: ""
});

var p1 = new Person({
    Id: 1,
    FirstName: "Damiano",
    LastName: "Fusco"
});

var p2 = new Person({
    Id: 2,
    FirstName: "John",
    LastName: "Doe"
});

window.myVM = {
    People: ko.observableArray([p1, p2]),
    NewPerson: ko.observable(pBlank),
    AddPerson: function() {
        var newid = this.People().length + 1;
        var newItem = new Person({
            Id: newid,
            FirstName: this.NewPerson().FirstName(),
            LastName: this.NewPerson().LastName()
        });
        console.log(newItem.FriendlyName());
        this.People.push(newItem);
        this.NewPerson(pBlank);
    }
};

window.myVM.People.subscribe(function() {
    console.log('People changed');
});

window.myVM.NewPerson.subscribe(function() {
    console.log('NewPerson changed');
});

window.myVM.NewPerson().FirstName.subscribe(function() {
    console.log('NewPerson.FirstName changed');
});

Can somebody who has more experience than me with Knockout explain why the code is not resetting the two input textboxes, First Name and Last Name, after you click Add Person?

Note that I’m trying to reset them by calling this.NewPerson(pBlank) just after I’m done adding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:44:52+00:00Added an answer on June 14, 2026 at 3:44 pm

    The reason your fields are persisting is because your pBlank object isn’t getting new observables (i.e. pBlanks fields are updating as you change them). http://jsfiddle.net/ngFM9/2/.

    Here’s how I would do.
    http://jsfiddle.net/ngFM9/1/ (I made quite a few changes)

    HTML

            <div class="ui-widget">
                <div class="ui-widget-header" style="padding:5px">People</div>
                <div class="ui-widget-content" style="padding:5px;height:100%">
                    <ul class="selectSingle" id="ulPeople" data-bind="foreach: People">
                        <li class="ui-widget-content" data-bind="text: FriendlyName"></li>
                    </ul>
    
    
                    <form data-bind="submit: AddPerson">
                        <span>First:</span><input type="text" data-bind="value: fname"/>
                        <br/><span>Last:</span><input type="text" data-bind="value: lastname"/>
                        <br/><input type="submit" value="Add Person"/>
                    </form>
                </div>
            </div>
    

    JS

    function Person(id, fname, lastname) {
        this.Id = ko.observable(id);
        this.FirstName = ko.observable(fname);
        this.LastName = ko.observable(lastname);
        this.FriendlyName = ko.computed(function() {
            return this.Id() + ' ' + this.FirstName() + ' ' + this.LastName();
        }, this);
    }
    
    var p1 = new Person(1, "Damiano", "Fusco");
    
    var myVM = function(p1){
    
        this.People = ko.observableArray([p1]);
    
        this.fname = ko.observable();
        this.lastname = ko.observable();
    
        this.AddPerson = function() {
            var newid = this.People().length + 1;
            var newItem = new Person(newid, this.fname(), this.lastname());
            this.People.push(newItem);
    
            this.fname('');
            this.lastname('');
    
        }
    };
    
    ko.applyBindings(new myVM(p1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code http://jsfiddle.net/DTYEB/ after dropping the element into the dropbox if I put
I finally got some parts working here: http://jsfiddle.net/trXBr/5/ but when I put the code
I have this code http://jsfiddle.net/DanielMontenegro/7pdU4/3/ Now, supose that I want to put that map
This is my code http://jsfiddle.net/Cfa6c/8/ Basically what I need to do is get the
What am I doing wrong here ( http://jsfiddle.net/dsqBf/2/ )? I'm trying to put the
i want to put the id into the colorbox below this code nom works
Consider this jsfiddle: http://fiddle.jshell.net/maple/JbEJN/show/ (this is the result window, in order for replaceState to
So, this fiddle: http://jsfiddle.net/aXsWz/31/ actually works how I want things to work. However, that
I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
I put this code into all of my viewControllers: UIInterfaceOrientation orientation = [[UIApplication sharedApplication]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.