PositioningCursorInEditForm

Summary: How to position the cursor at the end in the edit form
Version: 2006-08-30
Prerequisites: pmwiki 2.1, javascript support
Status:
Maintainer:
Categories: Editing

Questions answered by this recipe

How can I position the cursor at the end in the edit form? How can I open the edit window and the cursor will be at the end of the text?

Shorter way

For PmWiki versions 2.2.0-beta54 and newer, edit the wiki page Site.EditForm. Change:

   (:input e_textarea:)

to:

   (:input e_textarea focus=1:)

--Petko

Alternative way

The code snippet below is a redefinition of $InputTags['e_textarea'] otherwise defined in scripts/forms.php. When using action=edit&cursor=end it will open the edit form and move the cursor to the end of the text, thanks to some nifty javascript code. If the browser has javascript turned off it works just as the normal action=edit.

It works in Firefox and IE6, please report performance for other browsers.

If you want the cursor always to jump to the end of text when editing, remove both "if" lines at the top, and the } bracket at the end. Then it is triggerd with every action=edit and cursor=end is ignored/not needed.

To make this into a one-click action link, insert a link similar to the edit link into PageActions or your page action menus, like {$Name}?action=edit&cursor=end| Edit at End]].

Add to config.php:

# jump to end of edit textarea when ?action=edit&cursor=end
if (isset($_GET['cursor'])) $cs = $_GET['cursor'];
if($cs=='end') {
   XLSDV('en', array(
     'ak_save' => 's',
     'ak_saveedit' => 'u',
     'ak_preview' => 'p',
     'ak_textedit' => 'e',
     'e_rows' => '23',
     'e_cols' => '60'));
   $InputTags['e_textarea'] = array(
     ':html' => "
     <textarea \$InputFormArgs 
       onkeydown='if (event.keyCode==27) event.returnValue=false;' 
       >\$EditText</textarea>
     <script language='javascript' type='text/javascript'><!--
         el = document.getElementById('text');
         ln = el.value.length;
         try { el.focus(); }
         catch(e) { el.focus(); }
         el.scrollTop = el.scrollHeight;
         if (el.createTextRange) {
             var rg = el.createTextRange();
             rg.collapse(true);
             rg.moveEnd('character',ln);
             rg.moveStart('character',ln);
             rg.select();
         }
     --></script>
        ",
     'name' => 'text', 'id' => 'text', 'accesskey' => XL('ak_textedit'),
     'rows' => XL('e_rows'), 'cols' => XL('e_cols'));
}

Notes

Release Notes

  • 2006-08-30: Initial release

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

See Also

Contributors

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.