Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React to blur event in knockout.js

I have an input textbox and whenever it loses focus I want to get its value text in a function.

For example, if type "testimonials1", how could I get that text in an event handler for the blur event?

This is what I tried. I get ProjectTestimonial as an object, not user-typed text.

HMTL

<div class="ratingcontents" data-bind="foreach: ProjectTestimonial">
  <!--ko if: !Testimonialstext-->
  <input type="text" placeholder="Testimonials" class="txttestimonials" 
    data-bind="
      text: Testimonialstext,
      event: { 
        blur: $root.testimonialblurFunction.bind(SourceId, SourceText, Testimonialstext)
      }
    " 
  >
  <!--/ko-->
</div>

JS

self.testimonialblurFunction = function (data, event, Testimonialstext) {
    debugger;
    alert(data.soid + Testimonialstext);
}
like image 292
patel Avatar asked Jun 04 '13 10:06

patel


People also ask

How do you stop event blurs?

Preventing the blur If you want to prevent the blur event from being fired, you have to do so when you are inside the mousedown event, you can do so by invoking the method preventDefault() on the event. Click the checkbox, focus input & then click the button, the textfield never loses focus now.

What triggers blur event?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

Is blur a form event?

The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input> . In recent browsers, the domain of the event has been extended to include all element types.

When would an on blur event occur?

The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.


1 Answers

You can use event, that attaches to any JS event:

<input name="id" data-bind="value: id, event: { blur: blurFunction }">

And in your view model:

self.blurFuncion = function(){
    // this attacks when blur event occurs in input
}

Simple as that.

like image 96
Gabriel Anderson Avatar answered Oct 21 '22 04:10

Gabriel Anderson