Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea: can it detect the loss of focus

In HTML or Javascript is it possible to detect when a textarea looses focus?

I have a text area & on loosing focus I want to read the data in a textarea & check if its valid.

Maybe...

<textarea onloosefocus="myFunct();">
</textarea>

// or
var isTyping = false;

function onKeyUp( event )
{
   setTimeout( function()
   {
      isTyping = false;
   }, 100);

   setTimeout( function()
   {
      if (!isTyping)
         validateTextArea();
   }, 500);
}

function onKeyDown( event )
{
   isTyping = true;
}

<textarea onkeydown="onKeyDown(e);" onkeyup="onKeyUp(e);">
</textarea>
like image 364
sazr Avatar asked Jan 19 '12 02:01

sazr


People also ask

What event is triggered when a button for textarea element loses focus?

The onchange JavaScript event is triggered when an element is changed and then loses focus. In the context of a textarea , this happens when the content of the textarea is modified and then the textarea loses focus because the user clicks away or presses the tab key.

What is focus blur?

Events focus/blur The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an input field. In the example below: The blur handler checks if the field has an email entered, and if not – shows an error.

What is onfocusout?

The onfocusout event occurs when an element is about to lose focus. Tip: The onfocusout event is similar to the onblur event. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child loses focus, you should use the onfocusout event.

Which event is fired when an input text field loses focus?

The focusout event fires when an element is about to lose focus.


1 Answers

The event you are looking for is the blur event, available through the onblur attribute:

<textarea onblur="myFunct();"></textarea>

However, it's much better if you attach events properly, e.g. through jQuery:

$('#id_of_your_textarea').on('blur', function(e) {
    // your code here
});
like image 192
ThiefMaster Avatar answered Oct 21 '22 11:10

ThiefMaster