Announcement Announcement Module
Collapse
No announcement yet.
Defaultiing the focus to the first text field in IE Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Defaultiing the focus to the first text field in IE

    The Roo-scaffolded views use Dojo decorations for HTML form fields such as text fields, text areas, and select lists. I have a requirement that if any page has a text field on it, the focus should default to that field, so that the user can start typing straight away without having to click into it with their mouse (or use the Tab key).

    I've tried adding this script to the end of my default.jspx file, just inside the closing body tag:

    Code:
    <script type="text/javascript"><![CDATA[
                dojo.addOnLoad(
                    function() {
                        // Set the focus to the first editable field in the 1st form
                        var formElements = document.forms[0].elements;
                        for (var i = 0; i < formElements.length; i++) {
                            var formElement = formElements[i]; 
                            if (formElement.type == 'text') {
                                formElement.focus();
                                break;
                            }
                        }
                    }
                );
            ]]></script>
    This works fine in Firefox, as follows:

    http://dl.dropbox.com/u/493208/Images/ff_focus.png
    ... but not in IE8 (the main target browser for this app), where the first text field gets a different outline to the others, but doesn't accept keyboard input:

    http://dl.dropbox.com/u/493208/Images/ie_focus.png
    If I click in it or tab to it, it works like normal, but that defeats the purpose.

    Does anyone know how to properly give the focus to a Dojo-decorated text field?
Working...
X