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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:23:17+00:00 2026-05-28T19:23:17+00:00

I have the following view model which contains an array of elements function ReservationsViewModel()

  • 0

I have the following view model which contains an array of elements

   function ReservationsViewModel() {
        var self = this;


        self.availableMeals = [
            { mealName: "Standard (sandwich)", price: 0, id = 1 },
            { mealName: "Premium (lobster)", price: 34.95, id = 2 },
            { mealName: "Ultimate (whole zebra)", price: 290, id = 3 }
        ];  
  }

I want to bind this view model to a input, but i want to bind only the Single array element meal name that has the id value as the data-id attribute of the input.

<input type="text" id="firstElementMealName" data-id="1" data-bind="value: ??"></input>

In this example i would bind the array element with id = 1, and the text would appear as “Standard (Sandwich)”, but i still need the full binding and change tracking (observables) and all the other good stuff in Knockout.

How to pick up the data-id and use it in the binding code to bind the appropriate meal to the input?

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-05-28T19:23:18+00:00Added an answer on May 28, 2026 at 7:23 pm

    Suggested solution:

       function ReservationsViewModel() {
            var self = this;
    
    
            self.availableMeals = ko.observableArray([
                { mealName: "Standard (sandwich)", price: 0, id = 1 },
                { mealName: "Premium (lobster)", price: 34.95, id = 2 },
                { mealName: "Ultimate (whole zebra)", price: 290, id = 3 }
            ]);
    
            self.getMealById = function(id) {
                ko.utils.filterArray(self.availableMeals(), function(item) {
                    return item.id == id;
                });
            };
      }
    

    In the view:

    <input type="text" id="firstElementMealName" data-bind="value: getMealById(1)"></input>
    

    EDIT: here is a two-way solution:

    function ReservationsViewModel() {
            var self = this;
    
    
        self.availableMeals = ko.observable({            
            id_1: { mealName: "Standard (sandwich)", price: ko.observable(0) },
            id_2: { mealName: "Premium (lobster)", price: ko.observable(34.95) },
            id_3: { mealName: "Ultimate (whole zebra)", price: ko.observable(290) }
            });
      }
    
    ko.applyBindings(new ReservationsViewModel());
    

    In the View:

    <input type="text" id="firstElementMealName" data-bind="value: availableMeals().id_2.price()"></input>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a partial view I have the following: <%Html.RenderAction(MVC.User.GetComments(Model.UserGroupName)); %> can I render a
I have the following in my view: <%using (Html.BeginForm()) {%> <% foreach (var item
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have following ViewModel (TripSearchView) which contains IEnumerable(of AffiliateComponentTypeView) property, I have to populate
I have an ASP.Net MVC view which inherits from a ViewModel. The ViewModel contains
I have a solution with following projects GUI (contains views) ViewModels (contains all view
not sure how to word this correctly.. so i have a view which has
I have the following view model: Public Class MyViewModel Public Property SelectedIDs As List(Of
I have a view model for a view which displays multiple forms on the
I have a Create view which is passed a ViewModel. The ViewModel contains the

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.