Gautam
Gautam

Reputation: 1740

TabIndex - hitting tab moves me to Address Bar - unable to work around this using Focus or +tab indexes

I read several threads that talk about how the Address Bar in IE is basically the first one to get focus when using TAB (MSDN's own docs talk about this).

Yet, I have seen situations where this doesn't always have to be the case....

I have a master page and inside my content area is a formView.

It defaults to INSERT view and can never leave it (they can only insert not edit and reading is handled elsewhere)

So on my page load for the page I have:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If fvwLogEntry.CurrentMode = FormViewMode.Insert = True Then
            'Set the default field to position the cursor there...hopefully
            Dim FCtxtHrEmployeeId As TextBox
            FCtxtHrEmployeeId = CType(fvwLogEntry.FindControl("txtHrEmployeeId"), TextBox)
            Page.SetFocus(FCtxtHrEmployeeId.ClientID.ToString)
        End If

Now that works, when the page loads it sets the cursor to the employeeID text box inside the formview's INSERT template.

HOWEVER, when I hit TAB it takes me to the address bar and THEN if I hit tab again it takes me through the rest of the items on the page.

I set the tab index of the first item to 11 and then incrimented from there (I had read that IE's toolbars have tab indexes too so I thought perhaps using a higher number would bypass those, but again that doesn't REALLY make sense since it would still start at the lowest number, but I gave it a shot thinking it would move forward from where the focus was set.) If I click on the textbox and then hit TAB it DOES move through the page like I would expect.

It is just when the page loads and gets the focus set to the employeeID textbox that hitting tab moves it to the address bar.

I also tried setting the other controls to -1 (those I didn't want it to tab to), still no luck there.

So... what can I do to get around this?

There MUST be a simple way to set the focus to the employeeID textbox and ensure that pressing TAB after that moves to the next control in the formview's insert template and does NOT jump up to the address bar?

Upvotes: 10

Views: 18490

Answers (9)

Raviraj
Raviraj

Reputation: 21

I faced similar problem in IE. After some analysis I found that, this problem occurs if there is any HTML content outside form. for example:

<html>
 <div id="1">
 </div>
 <form>
//other code
 </form>
</html>

It worked for me, after I moved all HTML inside form tag.

<html>    
 <form>
 <div id="1">
 </div>
//other code
 </form>
</html>

Upvotes: 2

Kapil
Kapil

Reputation: 1

If you are using JSF or Primefaces, you can make use of:

<p:focus for"formname"></p:focus>

Upvotes: 0

mmamane
mmamane

Reputation: 363

I realize this is an old post, but an even simpler method is to add a "tab-stop" attribute to the form element with the last tabindex. Then bind a keydown listener and force focus to the first tabindex when the tab-stop is encountered.

Here's a simple example:

<input type="text" tab-stop />

$document.bind("keydown", function(event) {
    var attrs = event.currentTarget.activeElement.attributes;

    if (attrs['tab-stop']) {
        angular.element.find('select')[0].focus();
            event.preventDefault();
        }
    });
};

Upvotes: 1

Kevin W.
Kevin W.

Reputation: 44

I was having this issue as well. For me, it was being caused by the use of the .select() method in order to bring focus automatically on a text field as soon as the page loaded. I changed my code to instead use JQuery's .focus() method and that resolved the issue.

Upvotes: 2

Gautam
Gautam

Reputation: 1740

I found another better option which is fastest as of what I tried. Here's the code for that

function handleTabOrder() {
    $('.myClass :visible:input:enabled').each(function (index) {
        $(this).attr('tabindex', index + 10);
    });

    $('.myClass :visible:input:enabled:first').keydown(function (e) {
        if (e.keyCode == 9 || e.which == 9) {
            $("[tabindex=10]").focus();
        }
    });
}

What I have done here is to assign Tab order to all the visible controls on the page, then I have handled the key down event of only first control(that shifts the control to address bar) and now it shifts the control to next visible input item on the screen..

Its just a work around but works faster than any of the other things mentioned in the thread.

Just write the above function and all it in on-load event of page.

Upvotes: 2

Taylor Brown
Taylor Brown

Reputation: 1776

I had this same problem. It turns out mine was related to the ajax modal popup extenders. a modal popup was being shown, even though technically i could not see it because it was wrapped inside a parent div that was hidden. if you are using modal popup extenders, this could be causing an issue like this.

Upvotes: 0

Gautam
Gautam

Reputation: 1740

The answer mentioned in my other post works fine but it made the page take a huge performance hit because with every key press on the page the whole DOM was being searched for the elements. So I found a new more optimized solution

var myNameSpace = function(){

this.selector = '.myClass :visible:input:enabled:first';

this.myElement = $(selector);

this._body = $('body');
var _self= this;

this._body.on('keydown',_self.selector,function(e){
     if ((e.which == 9) || (e.keyCode == 9)) {
        _self.myElement.focus();
     }
   });
};

The general idea being to 'cache' the node to be accessed. No need to traverse the DOM again and again for just selecting.

Upvotes: 0

Gautam
Gautam

Reputation: 1740

The following jquery code seems to be working fine for me..

$(window).load(function () {
    $('.myClass :visible:input:enabled:first').focus();
});

$('body').on('keydown', '.myClass :visible:input:enabled:first', function (e) {
    if ((e.which == 9) || (e.keyCode == 9)) {
        $('.myClass :visible:input:enabled:first').focus();
    }
});

Upvotes: 2

Chris
Chris

Reputation: 2019

Have a look at: http://www.w3schools.com/tags/att_global_tabindex.asp

Your txtHrEmployeeId element should have tabindex 1 and all other elements should have higher values. -1 is not valid

Also verify that the tabindex are correct in the html that gets rendered (right-click in page and "view source").

Upvotes: 1

Related Questions