Note that the following technique assumes that you are developing your own module and that you are familiar with the basics of Drupal's Form API.
A Select All button offers a link that allows the user to instantly select the entire content of a "textarea" field in a form. Adding such a button or link insures that the user will grab the entire content of a given field before copying it to the clipboard. It provides added UI functionality to your application in cases where you need to distribute code, lists or other text-based snippets of data through a browser interface.
First, you will need to create a field of type "textarea." The following code defines a 50-column wide, 10-row high textarea field named "my_textarea":
$form = array(); $form['my_textarea'] = array( '#type' => 'textarea', '#title' => 'My textarea', '#description' => 'This is an XHTML textarea', '#cols' => 50, '#rows' => 10, );
Drupal will output the field to the client's browser wrapped in an textarea element with an id that corresponds to the keyname of the field prefixed with "edit-". For instance:
<textarea cols="50" rows="10" name="my_textarea" id="edit-my_textarea" class="form-textarea resizable"> My content </textarea>
Next, you need to define the "Select All" button:
$form['my_selectall_button'] = array( '#type' => 'button', '#description' => 'This button selects the entire content of the my_textarea field', '#value' => 'Select All', '#attributes' => array('onclick' => 'selectall("edit-my_textarea");return(false);'), );
drupal_add_js(drupal_get_path('module', 'my_module') .'/selectall.js', 'module');
- Overriding the user's clipboard is generally not considered a good or even safe practice.
- Copying and pasting to and from the clipboard is a very simple operation and should be familiar to the user. Where it is not so familiar, the added functionality would simply be useless.