Himmators
Himmators

Reputation: 15006

Trigger jquery function on blur unless specific elements are clicked

I have a login form. When the inputs get focused the "forgotten password" and "remember me" elements get shown by adding a css class, when they blur, the elements are hidden again by removing the class "sho". I would like the elements to keep the class "show"if I click either one of them or the login link

$(document).ready(function() {
    $('.login *').focus(showlogin);
    $('.login *').blur(hidelogin);
});


function showlogin(){
    $('.login .hidden').addClass("show");
}

function hidelogin(){
    $('.login .hidden').removeClass("show");
}

Html:

<form class="login">
  <input type="text"/>
  <input type="password"/>
  <a class="loginbutton" href="#">Log in</a>
  <br />
  <a class="hidden" href="#">Forgotten password</a>
  <label class="hidden"><input type="checkbox" /> Remember me</label>
</form>

Upvotes: 1

Views: 464

Answers (1)

Barmar
Barmar

Reputation: 780879

Instead of binding to blur, bind to a click outside the login form.

Here's some code I have in my page that closes my login box when I click outside it, you can adapt it to your needs:

    $(document).mousedown(function (e) {
        if ($(e.target).closest("#signin").length == 0) {
            $(".signin").removeClass("menu-open");
            $("fieldset#signin_menu").hide();
        }
    });

Upvotes: 1

Related Questions