Posted By

stiki101 on 01/21/08


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hans


Prototype function $


 / Published in: JavaScript
 

instead of document.getElementById('a');, you’d simply just do this instead:

$('a');.

And if you wanted a whole collection of elements, you can simply do this:

$('a','b',obj,obj2,'c','d');

  1. function $() {
  2. var elements = new Array();
  3. for (var i = 0; i < arguments.length; i++) {
  4. var element = arguments[i];
  5. if (typeof element == 'string')
  6. element = document.getElementById(element);
  7. if (arguments.length == 1)
  8. return element;
  9. elements.push(element);
  10. }
  11. return elements;
  12. }
  13.  
  14. // Sample Usage:
  15. var obj1 = document.getElementById('element1');
  16. var obj2 = document.getElementById('element2');
  17. function alertElements() {
  18. var i;
  19. var elements = $('a','b','c',obj1,obj2,'d','e');
  20. for ( i=0;i

Report this snippet  

You need to login to post a comment.