Welcome Guest [Log In] [Register]

Welcome to Big Boards. Big Boards at one time offered the Zathyus network our long famous Resource Archives, our extensive search indexes, and our info-packed resource forums. Eventually, these were made redundant by upgrades to the documentations and expanded search features added to the ZetaBoards software, and this forum then lost relevancy and subsequently became inactive.

Our team then embarked on a new project called Planet Nexus, where a bold new experiment in forum promotion is taking place. We call it nexworking. NexusTree offers social networking pages for forums in a fully automated format. Your NexusTree page is created instantly when your join the nexus. Find out more by visiting Planet Nexus, and become part of the nexworking revolution.

Posted Image Posted Image
If you're already a member please log in to your account for additional access:

Username:   Password:
Add Reply
Prototype dollar function in JavaScript?
Topic Started: Sep 2 2008, 09:10 AM (772 Views)
Posted Imageslayer766
Member Avatar
Can I haz a Gord?

Code:
 
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
 var element = arguments[i];
 if (typeof element == 'string')
  element = document.getElementById(element);
 if (arguments.length == 1)
  return element;
 elements.push(element);
}
return elements;
}

That's the function, I've only just recently seen this around while surfing the net, and I've NEVER seen anything like this before...

What this does, if you didn't know already, is that this function will grab an element the very short way if you have a bunch of elements to grab. So something like this:

If you were to have a code with a bunch of document.getElementById("rawr"); this function would simplify that so much more then just writing them all out and doing it by going through the dollar function like so:

document.getElementById("rawr") now becomes

$('rawr');
And NO this is NOT jQuery - it's plain JavaScript.

:O Was pretty interesting to me...
Mike is my coding brother.

Posted Image
Offline
Profile
Quote Post
Goto Top
 
Morgan
Member Avatar
Safe Boating Is No Accident

I know very little about JavaScript, but that's pretty cool. Perhaps jQuery uses a similar function in how it operates?

edit: Oooh, congrats on 200 posts by the way. :)
Posted Image

Posted Image
Offline
Profile
Quote Post
Goto Top
 
Posted Imageslayer766
Member Avatar
Can I haz a Gord?

Whoa someone commented. :P

Yeah jQuery does this, but in a different way.

JavaScript(With the dollar function available):

$('rawr').innerHTML="Hello";


jQuery:

$('#rawr').innerHTML="Hello";

Not(EDIT! I meant to say "Note" instead of "Not"...)e the difference between the two as jQuery references the # symbol, which is an ID. As the JavaScript way doesn't require that.
Mike is my coding brother.

Posted Image
Offline
Profile
Quote Post
Goto Top
 
HelenaZF
Member Avatar
advanced techno-feeb

I'm so impressed with you guys that understand this stuff... :)

Posted Image

Ginger = HelenaZF Posted Image
Posted Image Visit the Planet Nexus page!Posted Image

Posted Image
Download some of my graphic sets HERE
Offline
Profile
Quote Post
Goto Top
 
Posted Imageslayer766
Member Avatar
Can I haz a Gord?

I could always teach you if you wanted Helena.
Mike is my coding brother.

Posted Image
Offline
Profile
Quote Post
Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Programming & Web Design · Next Topic »
Add Reply