jQuery trigger select change value in data binding in site using Knockout.js - possible?


User 4561667

I need to activate select values ​​in a website using Knockout.js using a browser extension.

I'm using jQuery and I'm a little lost on the trigger ko method.

Here is the select where I want to change the value:

<select name="subDuration" class="select number_weeks" id="sub_duration"
data-bind="
    options: options.durationOptions, 
    optionsText: 'val', 
    optionsValue: 'key', 
    optionsCaption: 'Select subscription duration', 
    value:options.duration,
    event:{
        change: options.updateDuration,
        blur: options.validateDuration
    },
    attr:{
        'aria-invalid': options.hasDurationError,
        'aria-describedby': options.hasDurationError() ? 'sub_duration_error' : false
    }
" 
data-ctp-cont="Direct Debit Payments">
</select>

$("#sub_duration").focus().trigger("change", options).val(2).blur()

When I use the cde line above, it sets the value to 2, but the site validation runs on blur and doesn't recognize it as valid. If I choose something else and then choose 2 again it will. It feels like jQuery changes are not being triggered in the site ko js validation.

Any idea what I'm missing here? Sadly, I've never used knockout.js...

Any help is appreciated. Thanks!

User 4561667

Thanks to @user3297291, I followed the solution to inject js in the main site to be able to run code from my extension's content script.

Here's the solution code in my extended content script in case anyone has the same problem:

function myFunc() {
  //Trigger some Knockout JS functions on the main site scope.
}

var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ myFunc +')();'));
(document.body || document.head || document.documentElement).appendChild(script);

Thanks to anyone who has helped!

Related


About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

About data binding by knockout js and value change

Thomas There are two textboxes and a span bound by culling. The example I got is easy to understand. Here is the code. <p>First name: <input data-bind="value: firstName" /></p> <p>Last name: <input data-bind="value: lastName" /></p> <h2>Hello, <span data-bind=

Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {

Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {

Knockout JS - Binding select list to data object

Charlie Pope objective I wish to create a claim form. The claim form must support the following: Add (create) claim line Store (read) all claim lines Edit (update) claim line Delete (destroy) claim lines Display a variable number of fields based on user select

Knockout JS - Binding select list to data object

Charlie Pope objective I wish to create a claim form. The claim form must support the following: Add (create) claim line Store (read) all claim lines Edit (update) claim line Delete (destroy) claim lines Display a variable number of fields based on user select

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Select raw data value using JS/jquery

Well I am trying to select values from some raw data using jquery. I have tried something like this The var id = $("body").data("id"); original data looks like this {"photo":{"id":85456501,"user_id":601182}} Any ideas? Mathias You can get the raw data string i

Select raw data value using JS/jquery

Well I am trying to select values from some raw data using jquery. I have tried something like this The var id = $("body").data("id"); original data looks like this {"photo":{"id":85456501,"user_id":601182}} Any ideas? Mathias You can get the raw data string i