26

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

function test(){ var b=$('btn1'); eval(b); } 
2
  • 5
    In Javascript, $ is just another character to use when naming variables or functions, like _ or q. Commented Jan 30, 2010 at 12:09
  • 8
    I couldn't understand why you chose the Matteis' answer as correct. Your question is "What does $ function do in javascript?" so, it means that you would like to know what the function does....isn't it? Although, the answer that you put as correct answer a question like "Is $ a JS function or I am dealing with a JS framework/library?". My answer was good, being really generic, but the one that I thik is the most complete is the one posted by Vassallo. Commented Jan 31, 2010 at 12:47

7 Answers 7

67

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:

Sign up to request clarification or add additional context in comments.

1 Comment

This is not true. The $() function is native vanilla JavaScript. stackoverflow.com/questions/2167544/63670555#63670555
27

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

1 Comment

This is not true. The $() function is native vanilla JavaScript. stackoverflow.com/questions/2167544/63670555#63670555
2

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

1 Comment

2

Answering to your question, this function 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).

4 Comments

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)
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: mootools.net/docs/core/Element/Element 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!
@user10398534 that’s not true. These functions are only available in the browser’s dev tools. Please see this answer stackoverflow.com/a/22244912 Crome docs have a warning stating this difference developers.google.com/web/tools/chrome-devtools/console/…
-1

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

2 Comments

they asked about $, not $$, and this exists in mootools as well (mootools.net/docs/core/Element/Element#dollar)
-1

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

More details have a look at this article

3 Comments

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.
It's not jQuery if it's being used like $('btn1'). jQuery would do $('#btn1').
-6

The provided answers are simply not true.

$ defined, no jQuery

In this picture (taken on a Chrome about:blank tab), you can clearly see there is no jQuery. And given that the document is blank, there is no PrototypeJS as well. Contrary to all other answers, this is a native JavaScript function that can be used on any website.

The following picture shows the function definition.

$() definition

ƒ $(selector, [startNode]) { [Command Line API] } 

If this isn't convincing enough, here's the function definition from jQuery:

ƒ (a,b) {return new n.fn.init(a,b)} 

Here is the function in PrototypeJS.

function $(element) { if (arguments.length > 1) { for (var i = 0, elements = [], length = arguments.length; i < length; i++) elements.push($(arguments[i])); return elements; } if (Object.isString(element)) element = document.getElementById(element); return Element.extend(element); } 

I would disagree that this code is optimised appropriately, I would most certainly write it this way

function $(element) { if (arguments.length > 1) { let elements = []; for (let i in arguments) elements.push($(arguments[i])); return elements; //or simply `return arguments;` without the loop and extra variable } return Object.isString(element) && (e = document.getElementById(element)), Element.extend(element) } 

1 Comment

that’s not true. These functions are only available in the browser’s dev tools. Please see this answer stackoverflow.com/a/22244912 Crome docs have a warning stating this difference developers.google.com/web/tools/chrome-devtools/console/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.