I have a form with input fields and a button which fades in when you focus in any field.
I would like to make the button disappear when user clicks somewhere outside the form.
Just blur on input elements doesn’t work. As soon as you focus away of the input text field to press a submit button, the button disappears so you cannot press it.
I tried to have a blur method on form, but it didnt seem to work as well.
Thank you for any help.
$("#add_wall").click(function(){
$("#new_wall").animate({
opacity: 1,
width: '350'
}, 500, function() {
$("#send_button").fadeIn(400);
});
});
Form looks like
<form id="add_wall">
<input id="new_wall" type="text" placeholder="Compose new message..." />
<button id="send_button" style="width:60px; margin-left:5px;" >Post</button>
</form>
You should be setting your field itself to have the
.focus()listener, rather than the wrapping<form>.Blur should work on the field. You can’t “unfocus” from something that can’t have focus (like an
<a>or<input>).Example: http://jsfiddle.net/7p77p/
I don’t know what you’re trying to do with your field when someone “clicks” on the form (not a solid interaction with the form), so I’m going to ignore that for now. Your field should carry the focus and blur events, however, and your button should respond in kind.
Edit: Based on silly oversight on my part.
Then the way I would handle it is this: