views:

147

answers:

2

My blur event hides the submit button; but doing that seems to cancel the submit event. I only want to hide when the submit itself wasn't clicked. How can I determine if the focus was lost due to the submit, or due to just wandering off elsewhere?

<form action="" method="post">
    <textarea id="message" name="message"></textarea>
    <input type="submit" id="share" value="Share">
</form>
...

$('textarea').blur(function(){ 
     $('#share').hide() 
})
$('textarea').focus(function(){ 
     $('#share').show() 
})

Setting a timeout to allow the submit event to fire before coming back to the blur seems a bit hacky to me. Can't we do better than that? Can I tell the blur not to block other events? or search the dom for a pending submit event? or ... ?


Solution

for today is based on the ticked answer, but simpler. Use jquery's "fadeOut" routine to

  1. delay the hidden status of the submit button until after the submit event has fired, and
  2. make the user feel like their submission is being handled

.

$('textarea').blur(function(){ 
     $('#share').fadeOut() 
})
$('textarea').focus(function(){ 
     $('#share').fadeIn() 
})

It's indirect, and not really what I was looking for, but it seems clear that direct manipulation of the event queue - such as writing onBlur to say "if they did not click submit then hide the submit button" - is perhaps not technically possible.

+1  A: 

If you hide the Submit button, the submit will cancel.

Try making it completely transparent instead.

EDIT: For example:

$('#share').css({ opacity: 0, position: 'absolute' });
SLaks
nice idea; be nicer if the space it consumes were collapsed as well.
John Mee
@John Mee: set `height` and `width` to `0` ?
Felix Kling
@John: `position: absolute`.
SLaks
@Felix: or even just the height: "...css('height','0') ...css('height','40px'). Unfortunately this still hides it whilst the submit is waiting for a response, but getting closer :-)
John Mee
@Slaks: curiously playing with 'position' has the same behaviour as the hide; is the submit event blocked because it's not onscreen!?
John Mee
@John: Almost certainly not. Why don't you try it?
SLaks
+1  A: 

This is one option, though a bit hacky using jQuery .queue() and .clearQueue() to set an animation queue and instantly clear it before anything happens:

   $(function() {
     $("#message").blur(function() {
       $("#share").delay(100).fadeOut();
     });
     $("#share").click(function() {
       $(this).clearQueue();
     });
   });

Note: Requires jQuery 1.4+

Nick Craver
I like this one better than my answer. Much simpler.
patrick dw
This is basically the same solution as the setTimeOut; the fadeOut effectively delays that 'display:none' characteristic until after the submit event has fired. I don't know about old and tired machines but on mine the 'delay' and clearqueue is not necessary: I've simply altered the blur routine to | $("#share").fadeOut(); |. The submit is gone after the click but we've seen it fade out so it doesn't feel too uncomfortable as to "what's happening". I can't find any way to directly manage the events queue so I will settle for this for now. thx.
John Mee