views:

1825

answers:

5

How can I alias the function "document.getElementById" I've seen it done using $

Thanks

A: 

When you've seen the $() function, it was probably some library such as jQuery or Prototype. The $ function is not an alias for document.getElementById, but is a wrapper that extends the functionality of the function.

To create your own wrapper, you could write a function "alias":

var alias = document.getElemenyById;

or

function alias(id) { return document.getElementById(id); }

But really, I would use one of the libraries available such as jQuery or Prototype.

Marquis Wang
+1 but you should explain that the reason creating an "alias" is as simple as that is because javascript has first-class functions
annakata
under any conditions/use will this alias ever not work in IE6/7? I've had it bug out today in some older code and problem was resolved by replacing the alias with the document.getElementById. I actually used the second type of function "alias" above. Any ideas?
Nael El Shawwa
The 'var alias = document.getElementById' portion of this answer is incorrect. Creating an alias as demonstrated here does not work in Firefox or Google Chrome. See http://stackoverflow.com/questions/1007340 for more information.
Grant Wagner
+2  A: 

Here's something I use in my own library. DOM is the class and $ is a function which is a shortcut implementation of the getElementById function:

function DOM()
{
  this.$ = function(elementIDs)
  {
    var el;

    // If an array of element names is passed.
    if (arguments.length > 1) 
    {
      var elements = [];
      var length = arguments.length;
      for (var i = 0; i < length; i++)
      {
        // Call this function recursively for each passed parameter.
        elements.push(this.$(arguments[i]));
      }
      return elements;
    }

    // If a single element name is passed.
    if (typeof(elementIDs) == "string")
    {
      el = document.getElementById(elementIDs);
    }
    return el;
  }
}

Usage:

var el = new DOM().$("elementID");

You can alias it to the top level window element.

Cerebrus
A: 
function $( s ) {
    return document.getElementById( s ) ? document.getElementById( s ) : "";
}

$( "myId" );

This assumes you only want to use the dollar function (selector) to get an ID. I didn't test that, but it should work.

The last time I looked at the core of jQuery, it took a string as an argument and then used a regular expression to determine what kind of selector it was.

hal10001
@hal10001: This 'smells' horrible to me. Calling document.getElementById() twice seems wasteful and this function returns either an HTMLElement or a String. How about just 'return document.getElementById(s);' ?
Grant Wagner
You can return null instead of the string if you want. In jQuery, an empty jQuery object is returned if no ID is found. If you don't do something like that then you get undefined. All this really did was give an example of the dollar selector in action.
hal10001
A: 

Possibly the easiest/shortest way:

function $(id) { return document.getElementById(id); };
Chris Lloyd
It's not the shortest way, and certainly not the most efficient. It's totally unnecessary to apply the same context to a function that is applied anyway without `Function.apply`. Shortest form is `function $(s) { return document.getElementById(s) }`
Marcel Korpel
You are right. I thought that getElementById might take an optional second scope parameter but searching the spec (http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId) this isn't the case. Amended.
Chris Lloyd
A: 

facebook's connect-js does the following in src/core/prelude.js:

if (!window.FB) {
  FB = {
    // ...
    $: function(id) {
      return document.getElementById(id);
    }
    // ...
  }
}

Then, to use it, they do:

FB.$('name-of-id');

The initial block creates the global object FB. They use this object, FB, as a namespace and define all their objects, properties, and functions within it. That way, it will work with other JavaScript code as long as that other code doesn't use the global object FB.

MattDiPasquale
it is not an answer...
galambalazs
it is now an answer...
MattDiPasquale