0

This is a super newbie JS question, but am wondering how to use the '$' when writing JS. For instance, I wrote a very simple JS if statement:

if (document.getElementById('D').value == '') {document.getElementById('D').value = 0;}

However, I saw someone else write it like this:

if (document.getElementById('D').value == '') {$('D').value = 0;}

Does '$' equal 'document.getElementById'? What are best practices when using '$'? Thanks!

Dave
  • 1,257
  • 2
  • 27
  • 58
  • 1
    Possible duplicate of [What is the difference between JavaScript and jQuery?](http://stackoverflow.com/questions/20283098/what-is-the-difference-between-javascript-and-jquery) – BrTkCa Oct 26 '16 at 01:06
  • 1
    `$` is basically a identifier. In your second example, `$` is probably a reference to `jQuery` (a global function), being `jQuery` probably added by a library for JavaScript. –  Oct 26 '16 at 01:08
  • 2
    While `$` _usually_ refers to jQuery, it is possible for other libraries to use it (hence `jQuery.noConflict()`). `$` is a valid variable, just like `document`. Therefore, we can _assume_ `$('D')` is searching for a "D" element and returning it, but this doesn't really make sense in the context (personally, I've never heard of a "D" element). – c1moore Oct 26 '16 at 01:08
  • Possible duplicate of [What is the meaning of symbol $ in jQuery?](http://stackoverflow.com/questions/1049112/what-is-the-meaning-of-symbol-in-jquery) – Jim Stewart Oct 26 '16 at 01:08
  • Research effort :) – BrTkCa Oct 26 '16 at 01:11
  • 1
    @Someone - The second example is *not* jQuery (because jQuery objects don't have a `.value` property, and the argument is an element ID and not a CSS selector). – nnnnnn Oct 26 '16 at 01:50
  • @c1moore Anyone who's read One Piece knows of the "D" element :) – Dryr Oct 26 '16 at 03:32
  • @nnnnnn True, and the modern browsers have a global function called `$`, we just need to type $ in the console and test it (this global `$` looks like **`El#querySelector`**). The answer is wrong! Wrong as hell! –  Oct 26 '16 at 09:03
  • I think the second example could be wrong, anyway... if `$` may be a query selector probably, why wasn't a `'#'` declared before `'D'`? –  Oct 26 '16 at 09:11

4 Answers4

1

$ is usually used with JQuery. When you include JQuery in your page you can shorten references to it by using $('some css selector') to use it. If you didn't include JQuery you shouldn't need to use the $.

Here is a tutorial to get started with jquery: W3Schools JQuery Tutorial.

They show some examples there.

Joel
  • 324
  • 3
  • 9
  • w3schools is not necessarily a [good source](http://www.w3fools.com/) of information. I typically suggest avoiding it (although it may have made improvements, W3Fools seems to suggest it still has mistakes). – c1moore Oct 26 '16 at 01:14
0

$ typically refers to jQuery. It is a widely used library that helps with DOM manipulation. Your second example would only work if you included jQuery in your project (or you defined $).

wjohnsto
  • 4,323
  • 2
  • 14
  • 20
  • *"Your second example would only work if you included jQuery"* - The second example would *not* work with jQuery. – nnnnnn Oct 26 '16 at 01:46
0

The DOLLAR sign is shorthand for "jQuery". Anyplace you want to use a jQuery function you can substitute "$" for "jQuery".

... engage blithering idiot mode ...

I'm constantly referring to my copy of the O'Reilly jQuery Pocket Reference. That plus the docs on the jQuery site and the users on StackExchange make life easier. I still get tripped up when getting a single element from a jQuery function via brackets:

jQuery('div.topbar')[3]

That returns an HTML entity, not a jQuery object.

I use the debugger in Chrome a lot. I can do a ton of prototyping and POCs in the debugger before committing the work to code.

mnemotronic
  • 856
  • 1
  • 9
  • 16
  • 1
    *"The DOLLAR sign is shorthand for "jQuery""* - Except when it's not, e.g., if using MooTools or Prototype (or whatever). And not in the OP's example code either. – nnnnnn Oct 26 '16 at 01:48
0

It is basically used for the insertion of jQuery in your html code.

Deepak
  • 1
  • 1