24

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

function test(){
    var b=$('btn1');
    eval(b);
}
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
odiseh
  • 25,407
  • 33
  • 108
  • 151
  • 5
    In Javascript, `$` is just another character to use when naming variables or functions, like `_` or `q`. – ndim Jan 30 '10 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. – vfn Jan 31 '10 at 12:47

7 Answers7

66

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:

Julius
  • 91
  • 1
  • 8
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 1
    This is not true. The `$()` function is native vanilla JavaScript. https://stackoverflow.com/questions/2167544/63670555#63670555 – user10398534 Aug 31 '20 at 12:20
26

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

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • 1
    This is not true. The `$()` function is native vanilla JavaScript. https://stackoverflow.com/questions/2167544/63670555#63670555 – user10398534 Aug 31 '20 at 12:20
2

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

Pepijn
  • 4,145
  • 5
  • 36
  • 64
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).

BenMorel
  • 34,448
  • 50
  • 182
  • 322
vfn
  • 6,026
  • 2
  • 34
  • 45
  • 1
    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 Apr 08 '10 at 19:38
  • 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 Apr 10 '10 at 11:03
  • This is indeed a *native* function. https://stackoverflow.com/questions/2167544/63670555#63670555 – user10398534 Aug 31 '20 at 12:19
  • @user10398534 that’s not true. These functions are only available in the browser’s dev tools. Please see this answer https://stackoverflow.com/a/22244912 Crome docs have a warning stating this difference https://developers.google.com/web/tools/chrome-devtools/console/utilities – vfn Sep 01 '20 at 21:02
-1

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

Adir
  • 1,423
  • 3
  • 19
  • 32
-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

Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
  • 1
    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 Jan 30 '10 at 11:11
  • It's not jQuery if it's being used like `$('btn1')`. jQuery would do `$('#btn1')`. – ceejayoz Feb 08 '10 at 22:14
  • This is not true. https://stackoverflow.com/questions/2167544/63670555#63670555 – user10398534 Aug 31 '20 at 12:18
-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)
}
user10398534
  • 145
  • 14
  • 5
    that’s not true. These functions are only available in the browser’s dev tools. Please see this answer https://stackoverflow.com/a/22244912 Crome docs have a warning stating this difference https://developers.google.com/web/tools/chrome-devtools/console/utilities – vfn Sep 01 '20 at 21:02