Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  117] [ 2]  / answers: 1 / hits: 21445  / 9 Years ago, fri, april 10, 2015, 12:00:00

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?


More From » html

 Answers
58

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/


[#67120] Wednesday, April 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisw

Total Points: 625
Total Questions: 92
Total Answers: 88

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
ellisw questions
Mon, Aug 23, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 21, 20, 00:00, 4 Years ago
;