sazr
sazr

Reputation: 25928

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>

Upvotes: 11

Views: 21351

Answers (3)

ThiefMaster
ThiefMaster

Reputation: 318498

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
});

Upvotes: 18

Matthew
Matthew

Reputation: 25763

You can also use the onblur attribute for the textarea if you don't have access to jquery

Upvotes: 3

xdazz
xdazz

Reputation: 160833

Use jQuery will save you a lot of time, see .blur

Upvotes: 1

Related Questions