Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the MVC3 Razor equivalent of Page.Form.DefaultFocus?

If I wanted to conditionally set which field should have focus using ASP.NET WebForms, I would set Page.Form.DefaultFocus. What is the MVC3 Razor way to do the same?

Background: I have a login page. Sometimes the username field is automatically filled in, sometimes it's blank. If the username field is blank, I want it to have the focus. If the username field is filled in, I want focus on the password field.

like image 594
Rich Bennema Avatar asked Jan 28 '26 05:01

Rich Bennema


1 Answers

How about using jQuery:

$(function() {
    var username = $('#Username'); // TODO: adjust selector if necessary
    if (username.val() === '') {
        username.focus();
    } else {
        $('#Password').focus(); // TODO: adjust selector if necessary
    }
});
like image 59
Darin Dimitrov Avatar answered Jan 30 '26 20:01

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!