Search code examples
javascripthtmldata-bindingknockout.jsknockout-2.0

Same data-bind value for two observable in knockoutjs


This is my HTML Code:

<textarea name="Question" id="Question" data-bind="value:questionObj.questionText></textarea>

And this is my JavaScript Code:

questionObj = {
       questionText: ko.observable(' '),
       title:  ko.observable(' ')
};

questionText is data-binded to the textarea and the value is observable in bothway.I want questionObj.title to get the same value inside the questionObj.questionText. Is there any way to bind likewise ?


Solution

  • You mean you want both observables to have the same value? You can use a pureComputed observable for that.

    Change your observable definition like this:

    questionObj = {
       questionText: ko.observable(' ')
    };
    
    questionObj.title = ko.pureComputed(function(){
        return questionObj.questionText();
    }, questionObj);