Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable input - knockout

I would like to make my form editable after I'll click button.

I write code for a button click but I don't how to change state of the inputs in a form.

viewModel.test= function () {
   //code here
}
<input type="text"/> // Enable/Disable this

Can I disable/enable all inputs in the form or I just need to do it one by one?

like image 688
mskuratowski Avatar asked Apr 10 '15 12:04

mskuratowski


1 Answers

With pure knockout you can do this, basically toggling the isDisabled observable which updates the disabled attribute on the bound element. You can use knockout attr binding to set attributes on elements.

var ViewModel = function() {
    var self = this;
    self.isDisabled = ko.observable(false);
    this.disable = function(){
        self.isDisabled(true);
    }
    this.enable = function(){
         self.isDisabled(false);
    }
};

ko.applyBindings(new ViewModel()); 


<div>
    <input type="text" data-bind="attr : {'disabled' : isDisabled}"/> // Sets disabled attribute if isDisabled is true.
    <input type="text" data-bind="attr : {'disabled' : isDisabled}"/>
    <button data-bind="click : disable">Disable</button>
    <button data-bind="click : enable">Enable</button>
</div>

https://jsfiddle.net/xggu9Lv2/2/

like image 186
rjmacarthy Avatar answered Sep 21 '22 05:09

rjmacarthy