XForms/Setting Initial Cursor

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

When a form loads you want to set the initial cursor position so the user's first keystrokes are entered into the first field. This prevents the user from having to select the first field on a form with the mouse. Although some people consider this "polish" it is the hallmark of a conscientious developer.

Method[edit | edit source]

To perform this task you must do two things. First you need to give the control you want to receive the first keystroke event an id attribute. For example:

<xf:input id="first-field">

The next step is to use the xforms-ready event to set the focus to this field using the setfocus element.

These initial actions are usually placed at the end of the model in the html header but before the body.

   <xf:action ev:event="xforms-ready">
      <xf:setfocus control="first-field" />

Link to XForms Application[edit | edit source]

Load XForms Application

Source Code[edit | edit source]

   xmlns:ev="http://www.w3.org/2001/xml-events" >
      <title>Initial Cursor Positioning</title>
          <xf:instance xmlns="">

      <xf:action ev:event="xforms-ready">
         <xf:setfocus control="first-field" />
      <h3>Initial Cursor Positioning</h3>
      <xf:input ref="element1" id="first-field">
         <xf:label>Element One:</xf:label>
Next Page: Selection and Deselection Events | Previous Page: Events Overview
Home: XForms