0
votes

Is there some option to get all observable values from inputs? I mean to get this values:

<input type="text" data-bind="value: myValue">

I have many inputs with diffrent values, I need to get all of them and put them to the loop.

$("input[type=text]").each(function(){
       $(this).value().....
})

Any idias? Note: I need to get the observable value of my viewModel, not only the string

1
One of the points of using ko is avoid this kind of code, using the "MVVM" pattern that ko provide you should be able to iterate over the representation of your view (in this case your inputs), the famous ViewModel, i.e. for the following vm: myViewModel = { myValue: "foo", myValueFromInput2: "bar" ... n } just iterate over its properties, In resume, no there's no function on ko that provide what you want to do, just use the code that you provided "$("input[type=text]").each ... " - Roberto Alarcon
You are right. But I need to put some style on parent of this value if its not valid, i need to do this after clicking on some submit. Do you have another idia how can I call to knockout validation after clicking the submit? - LenaT
Or how can i put style on parent of input on blur? If its not valid - LenaT
@Jeff Mercado solution is nice. - Roberto Alarcon

1 Answers

2
votes

It is my understanding that you want to style elements with errors. If so, all you need to do is initialize validation with decorateElement set to true, set a css class you want to apply by setting the errorElementClass (validationElement by default) to the element and style it.

If invalid, the style will be applied to the element.

ko.validation.init({
    decorateElement: true,
    errorElementClass: 'error'
});

var viewModel = {
    // if missing, the 'error' class will be applied
    name: ko.observable('bob').extend({ required: true })
};

Here's a fiddle to demonstrate.