Prototype function $


/ Published in: JavaScript
Save to your folder(s)

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');


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.