instance method Form.Element#clear

View source on GitHub →

Form.Element#clear() → Element

Clears the contents of a text input. Returns the element.


This code sets up a text field in a way that it clears its contents the first time it receives focus:

  $('some_field').onfocus = function() {
    // if already cleared, do nothing
    if (this._cleared) return
     // when this code is executed, "this" keyword will in fact be the field itself
    this._cleared = true

This method can be called either as an instance method or as a generic method. If calling as a generic, pass the instance in as the first argument.