Issues with knockoutjs "checked" binding functionality in Safari and IE browsers

Need help figuring out why my pricing calculator for different document types is not working properly in Safari and IE browsers. The calculator gives you a final price based on the number of pages, quality, and quantity in your document.

The pricing data is generated by a service and outputted as a JSON object which is then used on the calculator. However, I have found that there is an issue with the Quality radio buttons specifically when using Safari browser.

In Safari, the pricing is always incorrect when selecting the "Quality" radio buttons. It seems that the first click on the radio button does not register correctly, resulting in the wrong price being displayed.

I've provided an example on jsfiddle to demonstrate the issue: https://jsfiddle.net/IntricatePixels/f21dtr8j/

Feel free to refer to the JSFiddle example for more details. If you need additional information, I'm happy to provide it here as well.

<div class="form-group document-quality">
    <label>Quality</label>
    <fieldset data-role="controlgroup">
        <div class="field-container" data-bind="foreach:categoryOptions, valueUpdate: 'afterkeydown', event: { change: onSubmit }" id="documentQuality">
            <label class="btn btn-default document-quality-label" data-bind="css: { 'active': $parent.selectedCategoryValue() === value }"></label>
            <div class="radio-container">
                <label class="btn btn-default document-quality-label" data-bind="css: { 'active': $parent.selectedCategoryValue() === value }">
                    <input data-bind="attr: {value: value}, checked: $parent.selectedCategoryValue" id="uniqueQuestionName" name="uniqueQuestionName" type="radio">
                </label>
            </div>
            <label class="btn btn-default document-quality-label" data-bind="css: { 'active': $parent.selectedCategoryValue() === value }"><span data-bind="text: label"></span></label>
        </div>
    </fieldset>
</div>
<div class="form-group quantity">
    <label>Quantity</label>
    <input data-bind="value: copies, valueUpdate: 'keyup'" id="numberofCopies" type="text">
</div>

Answer №1

Although the code may appear complex, I must admit that thanks to your guidance, I have discovered some clever techniques to tackle challenges that I previously thought were impossible (even though I am skeptical about using them :D).

However, let's refocus on the main issue at hand. Let's assume you are currently tired of tidying up your code and wish to proceed with this task. The problem lies in this particular line event: { change: onSubmit }. When the function was invoked, the value of selectedCategoryValue had not been updated yet (please note: I only tested this on IE11, as I do not currently have access to Mac computers). The snippet of code responsible for assigning the value to selectedCategoryValue only took effect after the execution of the onSubmit function.

<input data-bind="attr: {value: value}, checked: $parent.selectedCategoryValue" id="uniqueQuestionName" name="uniqueQuestionName" type="radio">

I experimented with two methods that proved successful.

The first approach is a bit rough, which I do not particularly endorse due to personal reasons disliking setTimeout.

Integrate setTimeout within the onSubmit function.

self.onSubmit = function() {
        setTimeout(function(){
            self.response("<div class='priceLoading'>Loading</div>");
            var servURL = "https://prices.azurewebsites.net/price/ProductionUS/" + ko.utils.unwrapObservable(self.selectedCategoryValue) + "/" + ko.utils.unwrapObservable(self.pages()) + "/" + ko.utils.unwrapObservable(self.copies());
            $.get(servURL, function(response) {
                self.response(response.PriceFormatted);
            });
            console.log(servURL);
        }, 100)
    }

Check out the Fiddle link here.

The second method aligns more with Knockout practices by utilizing the subscription feature of selectedCategoryValue to automatically trigger the onSubmit function when its value changes.

Update the old subscribe callback of selectedCategoryValue to:

self.selectedCategoryValue.subscribe(function(newValue) {
        self.onSubmit();
    });

Furthermore, remove the change event that calls onSubmit:

<div class="field-container" data-bind="foreach:categoryOptions, valueUpdate: 'afterkeydown'" id="documentQuality">

Fiddle link available here.

Lastly, I strongly recommend upgrading your Knockout library (if feasible) so that you can take advantage of the new features like textInput to simplify your value, valueUpdate binding setup.

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

filtering elements with selectable functionality in jQuery UI

I'm trying to exclude the <div> children from being selected within this list item and only select the entire <li>. The structure of the HTML is as follows: <ul id="selectable"> <li> <div id="1"></div> ...

Gathering all the indicated whole numbers from a string and placing them in an array

Upon decoding the URL query using decodeURIComponent and splitting it, the resulting array looks like this: ["pcs_availability:Online", "price:[1500 TO 1999.99]"]. I am trying to extract the proper integers from this array as shown in [1999.99]. In some ca ...

Strange black backdrop in dialog component

Today I came across a rather peculiar issue and was wondering if anyone else had experienced it and found a solution. The problem is pretty straightforward - when I load up my Vue component with a dialog element from the Element-UI library, the background ...

Using the JQuery template with $.get function does not produce the desired result

Working on populating a table using a Jquery Template can be tricky. One challenge is incorporating a json file via an ajax call for the population process. $(document).ready(function() { var clientData; $.get("/webpro/webcad/lngetusuario", funct ...

Improve the nested async callback in the componentDidMount() function of a React component by moving it to a separate

My current approach involves utilizing react's componentDidMount(), which contains a substantial amount of code and performs two callbacks. Eventually, within the innermost callback (the second one), I trigger this.setState({}). Simplified Code clas ...

What is the functionality of this JQuery Popup?

I'm facing an issue with my JQuery Popup. When the "Login" button is clicked, it hides the Login popup but doesn't show the Sign Up popup. How can I make sure that clicking on "Login" hides the Login popup and shows the Sign Up popup accordingly? ...

"HTML and JavaScript: The search functionality fails when trying to search for a string that is sourced from an array

Could someone assist me with a comparison issue I'm encountering? I am trying to use the includes() method to compare a list in a table with a list of names stored in a string. Strangely, when I hardcode the string directly into the includes() method, ...

Retrieve the value of a JavaScript variable

Currently, I am working on developing a web application that utilizes the Google Maps Direction API. Within a variable named summaryPanel.innerHTML, I store essential details about addresses and distances. I need to utilize the distance data for additional ...

What are the most effective strategies for creating an API for a website that utilizes Ajax requests?

In my web application, I currently have web forms that submit the complete form for backend processing of necessary modifications. However, I am now looking to implement inline editing for specific data portions, leading to a proliferation of fine-graine ...

Check to see if it is possible to click an HTML div

Currently, I am engaged in Selenium and Robot Framework automated testing and in search of a method to automatically locate div tags that are capable of being clicked. The keyword Click Element works well when provided with a specific CSS selector, but ho ...

AngularJS fails to trigger window scroll event

Although I have come across similar inquiries regarding this issue, none of the solutions have proven to be effective for me. Currently, I am working with AngularJS and I am attempting to detect the scroll event. Despite trying numerous variations to capt ...

JSON script and datetime formatting procedures

Here is my PHP/JSON script from localhost: If you need to download the PHP files to edit them in your answers, you can find them here. The link to the JSON file is mentioned in large-schedule.js. Instructions on usage are provided. The script partially w ...

JavaScript validation is failing to return false when re-entering the password

My JavaScript validation code is working fine. Javascript: All fields return false when re-entering the password, but JavaScript does not return false if it's not working The Re-Enter password JavaScript code is failing to work... An alert is displ ...

Tips for retrieving the chosen value from an ajax.net ComboBox using javascript

Is there a way to extract the selected value from an ajax.net combobox using JavaScript for client-side validation? What would be the most effective method to achieve this? Thank you. This is how I managed to obtain the value: var combo = $get('ddl ...

List of dates in React Native for Today and Tomorrow

In React Native, I am looking to create a dynamic list that displays the next seven days starting from today (assuming today is Monday): Today - Tomorrow - Wednesday - Thursday - Friday - Saturday - Sunday - Monday <- My goal is to show a total of seve ...

The height map method for plane displacement is experiencing issues

The heightmap I have selected: Scene without grass.jpg map : Scene with grass.jpg map: https://i.sstatic.net/q6ScO.png import * as THREE from 'three'; import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'; import ...

Is it possible to implement a "load more" feature with JSON data using the getJSON method?

Currently, I am working on the task of extracting objects from a JSON file and arranging them in a grid layout similar to masonry using Salvattore. While I successfully utilized this helpful tutorial to retrieve the data, my next challenge is to implement ...

Vue's v-for modifies the initial element within the list

I currently have a vue pinia store called "cartStore": import { defineStore } from 'pinia' export const cartStore = defineStore('cart', { state: ()=>({ cart: [] }), actions:{ async updateQuantity(id,logged,inc){ ...

Leveraging CSRF Defense Mechanism with Ajax Requests in Phalcon

I am facing an issue with the CSRF protection component in Phalcon when using Ajax. HTML Form <form id="signup-form" onSubmit="onSignUpSubmit(); return false;"> <input id="username" type="text" placeholder="Username" /> <input id=" ...

Utilize the angular ui-bootstrap datepicker exclusively for inputting dates in your application

Is there a way to disable key input and require users to use the datepicker to add dates to the input field? Currently, users can both select a date from the datepicker and manually type or erase the date in the input field. I would like to disable manual ...