Assignment from last class


<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
</head>
<body>

<script>
// create 500 buttons. 
for(var i=0; i < 500; i++){                  // do not modify
   $('body').append("<button></button>");    // do not modify
}                                            // do not modify

// assign a number from 0 to 499 to the button
var n = 0;
$('button').each(function(){
   $(this).html(n);
   // this.innerHTML = n;  // or this line instead of the above
   n++;
});


</script>
</body>
</html>








Notes

Action/Reaction: Making Pages Come Alive with Events (p147)

So far, javascript and jquery seems to allow us to automate things but not necessarily make our web pages interactive.

"Events" are key to making your pages interactive and javascript is an 'event-driven' language (as well as an 'object-oriented' language).

Web browsers recognize things like


These are all 'events'. When a user presses the mouse button, that is an event (mousedown event). When the user releases the mouse, that is also an event (mouseup event). Then that concludes with the click event.
Notes

Events

Recall the grid.html example. The reason this worked is because it was responding to mouse clicks. In this example, I used javascript's mechanism to associate events to functions.
<button type="button" onclick="button_0()">0</button>
In javascript, it's called an onclick event.
// toggle box #0
function button_0(){
  $('div:first').toggle();
}


We'll look at how jQuery handles events. (Part of the problem with using javascript is that there are browser incompatibilities).

Here are some events that jQuery defines:


Mouse Events (p148)

Document/Window Events (p150)

Form Events (p151)

Keyboard Events (p152)
Notes

Using Events (p162)

jQuery captures the differences between browsers and provides a uniform programming interface

  1. Select one or more elements
  2.     $('p')
    
  3. Assign an event "handler"
  4.     $('p').click();
    
  5. Pass an anonymous function to the event handler
    //  ('p').click(); 
    //
    //    function(){ 
    //        $(this).slideUp(); 
    //    } 
    //
    
       $('p').click(function(){
         $(this).slideUp();
       } );
    




[jq_03_click.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
</head>

<body>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Yet one more Paragraph</p>
<button>reset</button>

<script>
   $('p').click(function(){
     $(this).slideUp();
   });

   $('button').click(function(){
      $('p').show();
   });
</script>

</body>
</html>
If you prefer, you could use regular (named) functions, instead of anonymous functions, like this:
function myslideUp(){
  $(this).slideUp();
}

function myShow(){
   $('p').show();
}

$('p').click(myslideUp);
$('button').click(myShow);


Now if you think of the page being ready as an event (thus, the event handler ready()), you should understand the following [jq_03_ready.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script>
$(document).ready(function(){
   $('p').click(function(){
     $(this).slideUp();
   });

   $('button').click(function(){
      $('p').show();
   });
});
</script>

</head>

<body>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Yet one more Paragraph</p>
<button>reset</button>
</body>
</html>

$(document).ready() function is a built-in jQuery function that waits until the HTML for a page loads before it runs your script.

  $(document).ready();           //calling a ready() function of a $(document) object

Then we are passing an anonymous function (function without a name) as an argument

  function(){} // this is an anonymous function

This results in this somewhat confusing code

  $(document).ready(function(){
      // code here;
   });

The short-hand for the above is the more cryptic:
  $(function(){
  });  


/*
 *
 *         $();   +  function(){}
 * 
 */
FYI The eqivalent in javascript (without jquery) would be:
  <script>
  window.onload = function(){ 
   // code here
  }
  </script>
Notes

hover() event (p162)

mouseover and mouseout events are commonly used together. (eg. when you mouse over a button, a menu might appear and disapper when you move the mouse off the button).
jQuery provides a shortcut for this.
$('p').hover(function1, function2);

jQuery's hover() function works just like other event handlers except that it takes two function as arguments. If you want to pass two anonymous functions, it would look like this [jq_03_hover.html]
$('button').hover( function(){
   $('div').show();
}, function(){
   $('div').hide();
});

Alternatively, you can create a named function and pass that instead [jq_03_hover2.html]
function showtext(){
   $('div').show();
}

function hidetext(){
   $('div').hide();
}

$(document).ready(function(){
    $('button').hover(showtext, hidetext);
});
Notes

The Event Object (p164)

Whenever the web browser generates an event, it records information abut the event in an "event object". The event object contains such information as (See Table 5-1 of textbook or the jquery api reference page):


For example, "pageX" and "pageY" event property gives you the x and y coordinate of the mouse when the event occured.
In jQuery the event object is passed as an argument to the (anonymous) function you put into the event handler.
$('body').click(function(evt){

})
When the anonymous function is called, the event object is stored in the evt variable. Since it is an object, you can access it's internal properties by using the dot syntax. (eg. evt.pageX, evt.pageY)

[jq_03_event.html]
<!DOCTYPE html>
<html>
<head>
<title>jQuery</title>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script>
$(document).ready(function(){

    $(document).click(function(evt){
        var xPos = evt.pageX;
        var yPos = evt.pageY;
        $('body').append("<div>click</div>")
       $('div:last').css("position", "absolute").css("top", yPos).css("left", xPos);
    });

});
</script>
</head>
<body>
</body>
</html>
[jq_03_event2.html]: recognizing target HTML tag
[jq_03_event3.html]: retrieving DOM object
Notes

Stopping an Event's Normal Behaviour (p165)

Some HTML elements have preprogrammed responses to events.



You can prevent the default behavior by using the preventDefault() function of the event object. [jq_03_preventdefault.html]
$('a').click(function(evt){
     evt.preventDefault();
});

You can return the value false from the event function to prevent the default behaviour as well.

$('a').click(function(){
       return false;
 });
Notes

Removing Events (p166)

off() lets you remove an event handler that you had previously assigned. [jq_03_off.html]
$(document).ready(function(){

$('button').mouseenter( function(){
   $('div').show();
});


$('button').mouseleave( function(){
   $('div').hide();
});


$('button').click( function(){
   $('button').off('mouseenter');
});


});
Notes

Lab

- MM: Ch5 Tutorial: Introducing Events (p155)
- MM: Ch5 Tutorial: A One-Page FAQ (p174)

- Using this file [jq_03_lab.html] as a starting point, write jquery code so that when button 1 is pressed, first item in the array (content 1) is put into the <div>. When button 2 is pressed, second item in the array (content 2) is inserted into the <div>. When button 3 is pressed...

- Study the code jq_03_scroll.v3.html and comment each line what it does. You may have to look up some of the functions unknown to you at http://api.jquery.com/. Then use it as a starting point to create something interesting.

Send instructor URL. (Don't attach it to e-mail please)
Notes