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 8187779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:40:36+00:00 2026-06-07T02:40:36+00:00

I have a html form for adding multiple addresses: http://i48.tinypic.com/jg2ruo.png This way If I

  • 0

I have a html form for adding multiple addresses:

http://i48.tinypic.com/jg2ruo.png

This way If I change the Address Type selection the entire form has to bind to the correct json address object:

var jsonAddresses = { Addresses:
            [
                { AddressType: 1, Address1: "", Address2: "",Province:"",City:"",State:"",PostalCode:"",Municipal:"" },
                { AddressType: 2, Address1: "", Address2: "",Province:"",City:"",State:"",PostalCode:"",Municipal:"" },
                { AddressType: 3, Address1: "", Address2: "",Province:"",City:"",State:"",PostalCode:"",Municipal:"" },
                { AddressType: 4, Address1: "", Address2: "",Province:"",City:"",State:"",PostalCode:"",Municipal:"" }
            ] 
         };

I have done this with Jquery with a lot of code actually but I want to know how can I do this with Knockout. The idea is instead of having a fixed json object with the 4 types of addresses i want to have only 1 json object and if I select an address type that is not in the array then the object is added and binded, if the address type already exists in the array then just bind it. then i can have a “remove” link that when clicked the selected address type object is removed from the array.

Thanks in advance.

  • 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-07T02:40:37+00:00Added an answer on June 7, 2026 at 2:40 am

    I’m guessing a little bit on this, because its not entirely clear. You want a single form for editing addresses, with a dropdown that lets you select which address you are editing. I’ve put a working fiddle together, but here are the important parts.

    You have a concept of an Address object, which is observable since you will be updating the values. Then you need a viewmodel to keep track of all the address, have some concepted of the selected address, and the ability to add new addresses. This is the part that wasn’t clear, so I just went with a New Address button. Let me know if you have something else in mind. Other than the list of states, and the initial address data (both which should come from the server) this is all the code, and as you can see knockout makes it pretty simple.

    HTML:

    <select data-bind="options: addresses, optionsText: 'typeName', value: selectedAddress"></select>
    <div data-bind="with: selectedAddress">
        Name: <input data-bind="value: typeName" /></br>
        Line1: <input data-bind="value: address1" /></br>
        Line2: <input data-bind="value: address2" /></br>
        City: <input data-bind="value: city" /></br>
        State: <select data-bind="options: $parent.states, value: state"></select></br>
        Postal Code: <input data-bind="value: postalCode" />
    </div>
    <button data-bind="click: addAddress">New Address</button>
    <button data-bind="click: deleteAddress">Remove Address</button>
    

    ​

    ViewModels:

    var Address = function(address) {
        this.id = ko.observable(address.AddressType || 0);
        this.typeName = ko.observable(address.TypeName || '');
        this.address1 = ko.observable(address.Address1 || '');
        this.address2 = ko.observable(address.Address2 || '');
        this.city = ko.observable(address.City || '');
        this.state = ko.observable(address.State || '');
        this.postalCode = ko.observable(address.PostalCode || '');
    };
    
    var App = function(addressList) {
        var self = this;
        self.states = states;
        self.addresses = ko.observableArray(ko.utils.arrayMap(addressList,
              function(i) { return new Address(i); }
        ));
        self.selectedAddress = ko.observable(self.addresses[0]);
    
        self.addAddress = function() {
            var newId = self.addresses()[self.addresses().length -1].id + 1;
            var newAddress = new Address({AddressType: newId});
            self.addresses.push(newAddress);
            self.selectedAddress(newAddress);
        };
    
        self.deleteAddress = function() {
            self.addresses.remove(self.selectedAddress());
            self.selectedAddress(self.addresses[0]);
        };
    };
    

    EDIT1: added remove button. This is for the demo, obviously you will want some safety logic when the array is empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data recieved from a simple HTML form like this: $newdata =
I have html like this: <div> <span style=display: inline-block;height:20px>20</span> <span style=display: inline-block;><img src=img/ex.png/></span> </div>
i have html form i am using <a href="#" onclick="document.aa.submit()"> instead of submit button
I have a following problem, I have HTML form that uploads a file with
My input tag started to go crazy... I have html form for input to
I have a HTML form that has certain fields which i am opening inside
I have a HTML form that accepts a comma separated list of tags, which
I have a html form <form action=process.php method=post> <input type=checkbox name=name[v1] /> <input type=checkbox
I have an html form and I need to populate one of the input
I have an HTML form which, when submitted by the user, will call a

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.