‘input’ lostfocus/onblur event in knockout

‘input’ lostfocus/onblur event in knockout

Asked on November 23, 2018 in knockout js.
Add Comment


  • 3 Answer(s)

       Using few ideas will work. One is  use the KO’s hasfocus binding:  http://knockoutjs.com/documentation/hasfocus-binding.html.

       Binding with a boolean observable also work, and then subscribe to it. In the subscription,If the value is now false,nothing will do for that.

       Something like: 

    self.FirstName = ko.observable(); self.FirstName.focused = ko.observable();

    self.FirstName.focused.subscribe(function(newValue)

    {   if (!newValue)

    {     //do validation logic here and set any validation observables as necessary   } });

       Bind against it like:

    data-bind="value: FirstName, hasfocus: FirstName.focused"
    

       This help to fire each time a user leaves the field no matter how they leave it and regardless of whether a change was done.

    Answered on November 23, 2018.
    Add Comment

       Doing  everything with the Knockout is impossible. It’s just a tool but  not the best tool for the job. Do direct binding like  done in JS, i.e.

    $('#FirstName').on('blur', function ()
    {
      // do something
    });
    

        For interact with view model, Use the ko.dataFor as described in the Knockout’s documentation on event delegation:

    $('#FirstName').on('blur', function ()
    {
      var data = ko.dataFor(this);
      // do something with data, i.e. data.FirstName()
    });
    
    Answered on November 23, 2018.
    Add Comment

    This code will work for this

    data-bind="event: { blur: OnBlurEvent }"
    
    
    Answered on November 23, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.