Dave Dave - 5 months ago 43
Javascript Question

When to use $ in JS

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!


$ 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.