How to add jQuery to your HTML page

Tutorial on how to add the jQuery library to your HTML pages.

Published: Monday, 2 May 2011

Modify your page source

Download jquery and add the relevant version to your site. In the following example it has been deployed to /static/jquery-1.5.2.min.js.

In your HTML head, add the following:

<script type="text/javascript" src="/static/jquery-1.5.2.min.js"></script>
<script type="text/javascript" language="javascript">
  var $ = jQuery.noConflict();
  $(document).ready(function() {
    init();
  });
  function init() {
  }
</script>

Now you can refer to jQuery using the global $ variable. The init function is called when the page loads. In this function you can register events on your UI elements and other startup or initialisation code.

If you prefer, you can name the $ variable JQ instead.