Archive

Posts Tagged ‘prototype’

Clear a form with javascript (prototypejs)

January 19, 2009 Leave a comment

The following snippet clears all fields in a form:

function apagarForm(formName) {
    $(formName).getElements().each(function(item) {
        if (item.name != "form_name" && item.type != "button" && item.type != "submit") {
            if (item.tagName == "SELECT") {
                item.selectedIndex = 0;
            } else if (item.type == "text") {
                Form.Element.clear($(item.id));
            } else if (item.type == "radio" || item.type == "checkbox") {
                item.checked = false;
            }
        }
   })
}

This uses prototype to iterate over the form fields.

Categories: web Tags: , ,