1

I am getting undefined error while pushing new values into a array.

html:

 <select id="temp" data-bind="options:Original"></select>
 <select data-bind="options:dynamicaarry"></select>

viewModel:

var ViewModal=function(items) {
    this.dynamicaarry=ko.observableArray(items);
    this.Original=ko.observableArray(['volvo','saab','mercedes','audi']);
};

ko.applyBindings(new ViewModal(['four']));
$("#temp").change(function() {
    this.ViewModal.dynamicaarry.push('six'); //throws undefined error});});
}

And is it possible to handle selectedindexchange event in knockout without using jquery ?

2

1 Answer 1

1

You are trying to call ViewModal in different context. So, first of all, you should remove this from change event handler, and the second, you shouldn't use jQuery change event handler, you should to use knockout binding, instead:

Html:

<select id="temp" data-bind="options:Original, event: { change: changeOriginal }"></select>

ViewModel:

var ViewModal=function(items) {
    this.dynamicaarry=ko.observableArray(items);
    this.Original=ko.observableArray(['volvo','saab','mercedes','audi']);

    this.changeOriginal = function() {
        this.ViewModal.dynamicaarry.push('six');
    }
};

ko.applyBindings(new ViewModal(['four']));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.