tags:

views:

428

answers:

6

What does the $() function do in the following example?

function test(){
    var b=$('btn1');
    eval(b);
}
+19  A: 

That's not part of ECMAScript (JavaScript). It's just a function defined by some library of yours. Usually jQuery or PrototypeJS.

Luca Matteis
+1  A: 

I think you're dealing with a framework here. Most frameworks include $ functions to generate custom objects from a selector or dom object.

Pepijn
+30  A: 

The $() method is not part of the JavaScript language. It is often defined in JavaScript frameworks such as jQuery and Prototype, as a DOM selector.

It is interesting to note that up to until December 2009, the ECMAScript specification used to state:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. (Source)

However this "dollar sign for mechanically generated code" hint was removed from the current ECMAScript specification (ECMA 262 - 5th Edition / December 2009).


Nevertheless, the original question was probably referring to the popular DOM selectors in jQuery, Prototype, et al. Here are a few jQuery examples:

$('*');         /* This selector is a wild card method and will select all 
                   elements in a document. */

$('#id');       /* This selector selects an element with the given ID. */

$('.class');    /* The class selector will gather all elements in the 
                   document with the given class name. */

$('element');   /* This selector will collect all elements in a document with 
                   the given tag name i.e. table, ul, li, a etc. */

You may want to check the following article for more examples:

Daniel Vassallo
A: 

Its not JS built in function, its Prototype
http://www.prototypejs.org/api/utility/dollar-dollar

Adir
they asked about `$`, not `$$`, and this exists in mootools as well (http://mootools.net/docs/core/Element/Element#dollar)
Dan Beam
A: 

$ sign is not part of javascript it is a part of a javascript framework probably jQuery

More details have a look at this article

Yassir
To make it a bit clearer, $ is just a variable name, just like if you had for instance `var b=A('btn1');` Most JS libs simply use `$`, since it is unlikely to conflict and it also stands out.
Svend
It's not jQuery if it's being used like `$('btn1')`. jQuery would do `$('#btn1')`.
ceejayoz
A: 

Answering to your question, this funtion return the DOM object with the specified ID.

For example, if you have on your HTML:


<div id="thisIsMyDivId">This is some content</div>

You can get the DIV element using:


var myDiv = $('thisIsMyDivId');

The idea of this function is to replace the necessity of use document.getElementById to do this.

And......repeating what everyone here already did...It is not a native JS function, it is implemented on some Frameworks (Prototype and jQuery AFAIK).

Cheers,
VFN

vfn
this is wrong because you're expecting MooTools or Prototype has overridden the `$` function. if I said `window.$ = function(){alert('foo!');return null;}` this would change the outcome a bit, wouldn't it? also, jQuery would require a # before the ID (i.e. `$('#id')` and return itself (jQuery), not the DOM Element itself (though this is accessible via a property on the jQuery object returned, i.e. `$('#id')[0]`, *if there is a result*)
Dan Beam
Hi Dan, you seem to be a little confuse. Please, have a look at the following links and so we will be on the same page: http://mootools.net/docs/core/Element/Element http://prototypejs.org/api/utility/dollar Please, only vote down when you are sure of what you are doing. Let me know if you have any queries or if you need some help on understanding the documentation of the JS frameworks. My example is not a jQuery example. I do know the syntax used on jQuery, as well as on the other frameworks, and about you? do you know? If not, the links that I put here will help you. Cheers!
vfn