jQuery jQuery Namespace ("jQuery" and "$")


jQuery is the starting point for writing any jQuery code. It can be used as a function jQuery(...) or a variable jQuery.foo.

$ is an alias for jQuery and the two can usually be interchanged for each other (except where jQuery.noConflict(); has been used - see Avoiding namespace collisions).

Assuming we have this snippet of HTML -

<div id="demo_div" class="demo"></div>

We might want to use jQuery to add some text content to this div. To do this we could use the jQuery text() function. This could be written using either jQuery or $. i.e. -

jQuery("#demo_div").text("Demo Text!");

Or -

$("#demo_div").text("Demo Text!");

Both will result in the same final HTML -

<div id="demo_div" class="demo">Demo Text!</div>

As $ is more concise than jQuery it is the generally the preferred method of writing jQuery code.

jQuery uses CSS selectors and in the example above an ID selector was used. For more information on selectors in jQuery see types of selectors.