Prototype: Enable/Disable Elements

  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.
  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.
  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.
  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.
  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.
  • : Function split() is deprecated in /home/kourge/kourge.net/modules/filter/filter.module on line 895.

I have written some code for Prototype. Prototype can toggle, show, hide, and remove elements (these are just one of the many features Prototype can do to elements).

I went even further: making it possible to disable, enable (if it's already disabled), and toggle disable elements. This is the code. (It's under the same MIT-style license as Prototype)

  //toggleDisable function. by kourge.
  toggleDisable: function() {
    for (var i = 0; i < arguments.length; i++) {
      var element = $(arguments[i]);
      if (element.hasAttribute('disabled')) {
		  element.removeAttribute('disabled');
	  } else {
		  element.setAttribute('disabled', 'disabled');
	  }
    }
  },
  //disable function. by kourge.
  disable: function() {
	for (var i = 0; i < arguments.length; i++) {
		var element = $(arguments[i]);
		if (element.tagName.toLowerCase()=='input') {
			element.setAttribute('disabled', 'disabled');
		}
	}
  },
  //enable function. by kourge.
  enable: function() {
	for (var i = 0; i < arguments.length; i++) {
		var element = $(arguments[i]);
		if (element.tagName.toLowerCase()=='input') {
			element.removeAttribute('disabled');
		}
	}
  },

Note that all of these functions support muliple elements, which means you can go like Element.disable('one', 'two', 'three'); and disable the elements that bear the id one, two, and three all together.

Actually, you can go even further and improve the Element.remove() function. The current (v1.3.1) Prototype doesn't support multiple elements to remove: it can only accept one element at a time. For those who do need to remove multiple elements at a time, replace the original remove() code...

remove: function(element) {
    element = $(element);
    element.parentNode.removeChild(element);
  },

With this...

remove: function() {
    for (var i = 0; i < arguments.length; i++) {
      var element = $(arguments[i]);
      element.parentNode.removeChild(element);
    }
  },

The improved remove() code above is also the same MIT-style license as Prototype.