Search code examples
knockout.jsdurandalknockout-validation

need to focus on the first control in DOM Tree which has error, using knockout-validation


I am validating an HTML using knockout-validation, I need to focus the cursor on the first control having error how could I do that?

Please help..

Thanks


Solution

  • Make sure you are setting an error class for your elements in the data-bind attrbute of all elements being validated:

    data-bind = " ...
                  validationOptions: { errorElementClass: 'input-validation-error' }
                  ... "
    

    Then at the point of validation you can get all of these elements with jquery and just apply focus to the first one:

    $(".input-validation-error").first().focus();