Exploring the power of knockoutjs through the checkbox checked binding

I can't seem to get the knockoutjs checked binding to function as intended. I'm not sure if I'm making a mistake or something else is causing the issue. Here is the HTML snippet I'm working with:

 <ul data-bind="foreach: ListItems" >
                        <li style="padding-left: 0px; margin-left: 0px; color: white; font-size: 12px;">
                            <div class="title" style="margin-right: 3em; line-height: 20px;">
                                <input type="checkbox" data-bind="checked: IsActive" />

                                <label data-bind="text: Quantity, disable: IsActive"</label>
                                <label data-bind="text: Description, disable: IsActive" ></label>

                            </div>
                        </li>
                    </ul>

I want users to be able to mark items off a list by clicking the checkbox, which should strike through the text or change its color. However, the checkbox doesn't seem to have any effect. My view model is generated from JSON data in this format:

{"$id":"1","Description":"New List","Categories":
[{"$id":"2","Description":"Bread/Bakery","ListItems":    
[{"$id":"3","IsActive":1,"Description":"Bread","Quantity":"1 Loaf"}]},
{"$id":"4","Description":"Beverages","ListItems":
[{"$id":"5","IsActive":1,"Description":"Coke","Quantity":"1 Case"},

The issue here is that checking the checkbox doesn't trigger any changes. It should disable the other labels, but it's not working properly. All other values are displayed correctly, and when I use data-bind="text: IsActive", I see the value changing with the checkbox but it doesn't affect the labels.

Edit: per suggestion below:

var mydata = ko.observableArray([
            {
                Categories: ko.observableArray([
                      {
                          Description: "Dairy", ListItems: ko.observableArray([
                          { Description: "Eggs", Quantity: "1 Dz.", IsActive: ko.observable(false) },
                           { Description: "Milk", Quantity: "1 Gallon", IsActive: ko.observable(false) }
                          ])
                      },
                      {
                          Description: "Produce", ListItems: ko.observableArray([
                              { Description: "Lettuce", Quantity: "1 Head", IsActive: ko.observable(false) },
                              { Description: "Oranges", Quantity: "5 ea.", IsActive: ko.observable(false) },
                              { Description: "Greenbeans", Quantity: "1 Thingy", IsActive: ko.observable(false) },
                          ])
                      },
                ])
            }
    ]);

Answer №1

The problem you are experiencing is due to the fact that your data is in plain JSON format, which only allows for one-time binding.

If you want to achieve bidirectional binding, you will need to make sure your objects are "observables". Here's an example:

var myData = ko.observable({
    Categories: ko.observableArray([
      { IsActive: ko.observable(true) }
    ])
});

Keep in mind that it's important for you to decide which parts of your structure should be observables based on the desired behavior of the bindings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Creating a Two-Column Layout with ASP.NET Core and Bootstrap through POST

I have been working on a simple form for testing and learning purposes. Initially, I used Scaffold New Razor Page in VS to create the form. Afterward, I attempted to modify it in order to have two columns on the create page. Despite trying various methods ...

In my HTML document, I have three identical Id's and I need to figure out how to target the third one using CSS. Can you help

I am working on an HTML document that contains multiple instances of ids and classes. Here is a snippet of the code: <!DOCTYPE html> <html> <body> <div id="Caption_G" class="editor-group"> editor-group<br /> ...

Tips on finding an error message within the website's developer options when the inspector fails to locate it

I'm dealing with a website at On this site, there's a 'get latest blog posts' field that I want to automate the error message for. When I input an invalid email address like test123.com, I receive an error response but can't find ...

Error: An unknown identifier was encountered unexpectedly when coding in Typescript for front-end development without the use of a framework

About My Current Project For my latest project, I decided to work on a basic HTML canvas without using any frameworks. To ensure type checking and because of my familiarity with it from React projects, I opted to use Typescript. Below is the simple HTML ...

How can the communication be improved between a JSP page and a Java object?

In the midst of a project involving a java back-end and web page UI, my goal is to integrate the two seamlessly. The java system functions on the server, storing rules created within the web page's interface. To achieve this, I am seeking a way for te ...

Extracting Data from Web Pages

How can I efficiently extract data from a webpage with dynamic loading using AJAX? For instance, imagine a webpage that initially shows 20 images, but as the user scrolls down, more images are loaded (similar to Facebook). In this scenario, how would you ...

Overflow scrolling container with additional space to the right of an image

I am currently working on a project that involves a specific website. When examining the main battalion image, I noticed there is an excessive 300px of whitespace on the right side that I am struggling to remove. There is no hidden content or padding caus ...

A step-by-step guide on incorporating a div panel in HTML and JavaScript triggered by a submit button click within an HTML form

For the past 3 to 4 days, I've been grappling with an issue. I've put together a student registration form using HTML and JavaScript. The form includes validation through JavaScript and string concatenation to submit all form values and display s ...

Retrieve the stylesheets located within the <head> section of an external webpage and load them onto the current page

Trying to figure out a solution, I received an interesting request from a friend. They asked for help in creating a webpage that can dynamically pull a div tag from an external page (on a different server). To accomplish this, I utilized jquery's .loa ...

Is it possible to insert clickable links within the content of a Twilio text message?

Currently, I am utilizing Twilio and Express to send programmable SMSs to the users of my web application. I'm curious if it's possible to include hyperlinks within the body of these text messages. Is there a method to achieve this? I have attem ...

Trouble getting inline SVG to scale properly with its parent

Attempting to utilize SVG as an alternative to clippath due to limited cross-browser support has proven challenging. Each time I make the attempt, I face the same issue: the SVG functions properly but appears small and does not scale to fit the designated ...

Achieve a visually appealing layout by stacking multiple centered divs within Bootstrap columns using CSS

For a section on my website, I need to create three responsive columns where the ".service-icon" elements are centered in each column. The background image for the columns is currently set as dirtcolumn.png but can be replaced with a full-height div as lon ...

DjangoPDFViewer

I need help figuring out how to integrate a PDF Viewer into my Django website. Right now, I'm using an iframe to show the PDF, but I want to add annotation functionality directly on the site. I've looked into using PDF.js, but it means incorporat ...

Retrieving an image from an array

I attempted to design a slider for displaying my projects by creating an array to fetch information and insert it into HTML. However, I encountered difficulty in getting the images to change as only the text and heading were updating. Despite seeking assis ...

Using React.js to dynamically display or hide elements generated within a component

I'm currently working on a project where I need to dynamically generate form fields based on certain criteria. Specifically, I want to hide some fields and only show them when other specific conditions are met, a common requirement in form designs. Fo ...

Animation for maximum height with transition from a set value to no maximum height

While experimenting with CSS-transitions, I encountered an unusual issue when adding a transition for max-height from a specific value (e.g. 14px) to none. Surprisingly, there is no animation at all; the hidden elements simply appear and disappear instant ...

Table is not updating correctly following jQuery adjustments on Google Chrome

After resolving one of my previous questions mentioned in this solution, I encountered display issues in Google Chrome following recent changes. The table header row was not refreshing correctly after JavaScript modifications, resulting in anomalies. An e ...

What significance does the symbol "'" hold in the ng-style attribute?

Can someone explain the purpose of the " \' " in this code snippet? <div ng-style="{ \'cursor\': row.cursor }" Is there a reason why it can't be written simply as <div ng-style="{ cursor: row.cursor }" Here is ...

How to Use CSS to Position an Image Within a Div?

.framework { position: absolute; } .headerimg { margin: 0 auto; } <body> <div class="framework"> <div class="header"> <div class="headerimg"> <img src="Header.jpg"> ...

Creating a single HTML file with multiple pages, each without a fixed navigation bar on the page

The main page will feature just a few buttons that will display specific content to the user. These buttons within #home will not be in the header, so they will only be visible on the #home section. <section id="home"> <a href="#content">co ...