From Progzoo
Revision as of 10:36, 2 January 2013 by Andr3w (Talk | contribs) (Hello World)

Jump to: navigation, search

Introduction to jQuery and JavaScript

Hello World

Create two files yourself; q1.html and q1.js – these have the following content:

<!doctype html>
<script src="jquery.min.js"></script>
<script src="q1.js"></script>
  alert('Hello World');

Hello jQuery.png

Download the jQuery file from and save it in the same directory. Load q1.html into a web browser:

Adding Content

The $ function is called once the main page elements are loaded. You can build content in the ready function or you can examine the existing page when ready:

  $('body').append("Here is a button:");
  $('body').append($('<button/>',{text:'Click me'}));

You can make your button do something:

  $('body').append("Here is a button:");
  $('body').append($('<button/>',{text:'Click me',
      alert('Hello World');

Programming some content

The following example creates some words and images. You can get the gif images from

var cl = [
   {name:'Italy', src:'it.gif'},
   {name:'South Africa',src:'za.gif'}
  for(var i=0;i<cl.length;i++){

You can add style to the elements (see question 4 for how to do this). The name of each country has been put in a div. The div has the fontSize set to 200% The div has topMargin set to 2em

The images have an 8px gray border.

In this example each country has a div that floats left. Inside each div is another div with the country name and an image for the flag. The flag height has been set to 100px The outer div has a margin of 1em a background color of #BBB and a fixed with of 220px.

Note that the file contains cl.json – you can use this to show hundreds of flags.