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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:25:16+00:00 2026-06-17T06:25:16+00:00

I have seen so many example of this problem but not as a list.

  • 0

I have seen so many example of this problem but not as a list. Here’s my code:

<div>
<form>
    <p>You have asked for <span data-bind='text: gifts().length'>&nbsp;</span> gift(s)</p>
    <input id='giftname' type='text'/><button data-bind='click: createGift'>Add Gift</button>
    <table>
        <thead>
            <tr>
                <th>Gift name</th>
                <th>Pack</th>
                <th>Price</th>
                <th />
            </tr>
        </thead>
        <tbody data-bind='foreach: gifts'>
            <tr>
                <td><input data-bind='value: name' readonly='readonly'/></td>
                <td><select data-bind="options: packs,
                                        optionsText: 'pack',
                                        value: price" /></td>
                <td><input data-bind="value: price ? price.packprice : 'unknown'" readonly="readonly"/></td>
                <td><a href='#' data-bind='click: $root.removeGift'>Delete</a></td>
            </tr>
        </tbody>
    </table>
    <button data-bind='enable: gifts().length > 0' type='submit'>Submit</button>    
</form>
</div>
<script type="text/javascript">
    var GiftModel = function(gifts) {               
        var self = this;
        self.gifts = ko.observableArray(gifts);     
        self.addGift = function(gift) {
            var newGift = {
                name: gift.name,
                packs: gift.packs,
                price: gift.price
            };  

            self.gifts.push(newGift);           
        };

        self.removeGift = function(gift) {
            self.gifts.remove(gift);
        };

        self.createGift = function() {
            var gname = $('#giftname').val();
            //should be getting gift options from webservice
            var newGift = {name: gname, 
                            packs: [{pack:'Each', packprice: '2'}, {pack:'Dozen', packprice: '12'}], price: {pack:'', packprice: ''}};
            self.addGift(newGift);          
            $('#giftname').val('');
        };
    };

    var viewModel = new GiftModel([]);
    ko.applyBindings(viewModel);        
</script>

When I add gifts, it creates options of packs. Each pack has a certain price. My problem is just simple. How will I show the price on the next column for the selected pack of the gift line? Sorry im just new to knockoutjs. Thanks!

  • 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-17T06:25:18+00:00Added an answer on June 17, 2026 at 6:25 am

    System will automatically update price after selecting package if you make it observable. I made a little refactoring to you code, now it works:

    <div>
      <form>
        <p>You have asked for <span data-bind='text: gifts().length'>&nbsp;</span> gift(s)</p>
        <input
        id='giftname' type='text' data-bind='value: giftName' />
        <button data-bind='click: createGift'>Add Gift</button>
        <table>
          <thead>
            <tr>
              <th>Gift name</th>
              <th>Pack</th>
              <th>Price</th>
              <th />
            </tr>
          </thead>
          <tbody data-bind='foreach: gifts'>
            <tr>
              <td>
                <input data-bind='value: name' readonly='readonly' />
              </td>
              <td>
                <select data-bind="options: packs,
                                            optionsText: 'pack',
                                            optionsValue: 'packprice',
                                            value: price" />
              </td>
              <td>
                <input data-bind="value: price() || 'unknown'" readonly="readonly"
                />
              </td>
              <td><a href='#' data-bind='click: $root.removeGift'>Delete</a>
              </td>
            </tr>
          </tbody>
        </table>
        <button data-bind='enable: gifts().length > 0' type='submit'>Submit</button>
      </form>
    </div>
    
    function GiftModel(name) {
      var self = this;
    
      self.name = ko.observable(name);
      self.price = ko.observable();
      self.packs = ko.observable([{
        pack: 'Each',
        packprice: '2'
      }, {
        pack: 'Dozen',
        packprice: '12'
      }]);
    }
    
    var ViewModel = function (gifts) {
      var self = this;
    
      self.gifts = ko.observableArray(gifts);
      self.giftName = ko.observable();
    
      self.removeGift = function (gift) {
        self.gifts.remove(gift);
      };
    
      self.createGift = function () {
        self.gifts.push(new GiftModel(self.giftName()));
      };
    };
    
    var viewModel = new ViewModel([]);
    ko.applyBindings(viewModel);
    

    Here is working fiddle: http://jsfiddle.net/vyshniakov/pzJaH/

    P.S. Don’t use jQuery to get field value if you using knockout. It is much easier to do this with knockout.

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

Sidebar

Related Questions

I have seen many answers for this type of question but its not related
I have seen many reportlab graphing examples. Generating the graph is not the problem,
I have seen this problem arise in many different circumstances and would like to
I have seen this asked before and seen many answer there is a facebook
I have seen many posting this issue in SO. I have gone through those
I have seen many question and applied their results but nothing happens I still
I have seen many articles about POCO. What is this?
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I have been working with IE6 for many years [sob], but have never seen
I have a problem practically identical to this question , but I'm looking for

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.