Skip to main content

How not to lose focus on a login page


I have a simple login form with 2 input fields: "username" and "password". "username" field is focused by default. The problem is that when user clicks outside "username" or "password" fields, the focus is gone (it is neither on "username" nor on "password" fields"). How can I force the focus to be on these 2 fields only ?



In my case, this is a really annoying behavior, so I really want to do this :)



Can I do something like:




$("*").focus(function() {
if (!$(this).hasClass("my_inputs_class")) {
// How to stop the focusing process here ?
}
});



?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. It sounds like you always want one of your inputs to be focused, fair enough. The way I would do this is to bind each of your inputs blur() events so that if it occurs, it goes to the next element.

    Here's the markup:

    <body>
    <form method="POST" action=".">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="submit" name="submit" />
    </form>
    </body>


    And here's the jQuery:

    $(document).ready(function() {
    // what are the named fields the user may focus on?
    var allowed = ['username', 'password', 'submit'];
    $.each(allowed, function(i, val) {
    var next = (i + 1) % allowed.length;
    $('input[name='+val+']').bind('blur', function(){
    $('input[name='+allowed[next]+']').focus();
    });
    });
    $('input[name='+allowed[0]+']').focus();
    });

    ReplyDelete
  2. You could use javascript to set the focus on focusout, but you really shoudn't. Forcing focus on those fields would break the normal interaction of the page. It would mean a user couldn't do something as simple as clicking on a link on the page, because focus would always be on those inputs.

    Please don't do it :)

    ReplyDelete
  3. If you really really want to do this (and you shouldn't) use delegate() instead of setting a separate event handler on every single HTML element:

    $('body').delegate('*', 'focus', function() {
    if (!$(this).hasClass("my_inputs_class")) {
    $('#username').focus();
    return false;
    }
    });


    But consider that this will make unacessible all elements except the two input fields via keyboard navigation (including the submit button, any links on the page etc.)

    ReplyDelete
  4. Set blur event handler so it brings back focus to one of your inputs.

    ReplyDelete
  5. You could do like this (in psuedo code):

    if user || pass blured
    if user.len > 0
    pass.setFocus
    else
    user.setFocus


    Hope you get it.

    ReplyDelete
  6. Install an onClick handler on the body element (or a div that covers most of the page). Clicking on the div should then set the focus on the username/password field.

    I also suggest to bind Return in the "username" to "Set focus on password" and Return in the "password" field to "Submit form".

    ReplyDelete
  7. You could enable the click on links by re-focusing on you inputs after a minimum time interval.

    Every time an object gains the focus you check if it has the required class: if not set the focus to the first of the form inputs; this way:

    <html>
    <head>
    <title>Example</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js" />
    <script type="text/javascript" >
    $(function() {
    $('*').focus(function() { var obj = this; setTimeout(function () {checkFocus(obj)}, 1)});
    })

    function checkFocus(obj) {
    if (!$(obj).hasClass('force_focus')){
    $('input.force_focus:first').focus()
    }
    }
    </script>
    </head>
    <body>
    <a href="http://www.google.com">Extrnal link</a>
    <form><div>
    User: <input type="text" name="user" class="force_focus"/><br/>
    Password: <input type="password" name="password" class="force_focus"/><br/>
    Other: <input type="text" name="other" class=""/><br/>
    <input type="submit" name="submit" value="Login" />
    </div></form>
    </body>
    </html>

    ReplyDelete
  8. this is only an idea, you can use a setInterval function to put the focus in the username field, and you can interrupt it when you want with clearInterval function.

    var A = setInterval(function(){ $('#username').focus();}, 100);


    ... and for interrumpt it, you can do something like this

    $('#password').focus(function(){ clearInterval(A);});

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex