2
votes

I want to pass a data-bind value as function parameter.

Here is sample code:

<data-bind="click: function (data) { myFunction('param1', data) }">

this is my view model

var vm = {
Category: ko.observableArray(),
Product: ko.observableArray(),
CatID: ko.observable("113"),
save: saveChanges,
Filter: FilterProduct,
};

CatID is an observable so there are some functions who change the CatID when they are called. and i want to pass this CatID as a parameter when specific button clicked.

Please help how can i do this.

1
please provide more code to explain how you get param1. Is it a user input? is it a user selection value? etc.. - Adel Sal
var vm = { Category: ko.observableArray(), Product: ko.observableArray(), Parameter: ko.observable("113"), save: saveChanges, Filter: FilterProduct, }; - Hamad
i have edit the question - Hamad

1 Answers

5
votes

You can pass the observable in the data-bind like this

<button data-bind="click: myFunction(CatID)">Test Click</button>

And define your function in your ViewModel:

var vm = {
        Category: ko.observableArray(),
        Product: ko.observableArray(),
        CatID: ko.observable("113"),
        myFunction: function (obs) {
            alert(obs());
        }
    };

I have created an example here: http://jsfiddle.net/nyothecat/QruPg/