Monday, October 20, 2014

Mouse Events

Hover Event


The hover event can take two handler functions as parameters: one for the mouseenter event and another for the mouseleave event.


Project Structure



If your .js file is included in several HTML files, your browser only asks for it once. It stores it in the browser cache so it doesn’t have to ask the server for the file every time we go to another HTML page that references your script file.


jQuery Methods

$.contains

This is a static method of the jQuery library that takes two parameters.
(A static method is the one that does not need a selector to call the method, only the jQuery name or its shortcut $.)

It checks all the child elements of the first parameter, seeing whether it contains the second parameter.

$.contains(this, document.getElementById("has_discount"))

$(<selector>).each(function() {})

.each uses the selector that calls it and creates an array of elements identified by that selector. It then loops through each element in the array sequentially.


live/delegate/on

If you know a head of time that your element will behave like some other elements already created, use the live method. This will attach a handler to the event for all elements that match the current selector, now and in the future.

This will work for elements that have not yet been added to the DOM.

slideUp

slideDown

slideToggle

fadeIn


Events

Event listeners are the browser's way of paying attention to what a person does on a  page and tell the JavaScript interpreter if it needs to do something or not.

Event Categories

Browser Events
  • error
  • resize
  • scroll

Document Events
  • load
  • ready
  • unload

Form Events
  • blur
  • change
  • focus
  • select
  • submit

Mouse Events
  • click
  • dblclick
  • focusin
  • focusout
  • hover
  • mousedown
  • mouseenter
  • mouseleave
  • mousemove
  • mouseout
  • mouseover
  • mouseup
  • toggle

Keyboard Events
  • keydown
  • keypress
  • keyup

Binding an event

Method 1: Convenience Method

We use this method to add events to elements as the page is getting loaded. 



Method 2

We use this method just like Method 1, but we can also use it to add events to elements that get added to the page after it is loaded, like when we create new DOM elements 



Removing an event

To remove one event


$(#myElement).unbind('click');

To remove all events

$(#myElement").unbind();

Triggering an event

Use a trigger method combined with a selector

$("form").trigger('submit');

Saturday, October 18, 2014

Introduction

jQuery is a JavaScript library specialized for changing web page documents on the fly.

jQuery can change the web page from within the browser.

jQuery makes the DOM less scary. It is JavaScript, but a much more approachable version.

jQuery uses a selector engine. You ask for something with a selector, and the JavaScript interpreter asks the DOM to get it for you.

The JavaScript interpreter does not change the original HTML and CSS files. It makes changes to the DOM's representations of the page in the browser's memory.

Look into the browser


jQuery Function



jQuery selects elements the same way CSS does

CSS selectors select elements to add style to those elements; 
jQuery selectors select elements to add behaviour to those elements.

Element / tag selector 

$("h1").hide()

Class selector

$(".my_class").slideUp();

ID Selector

$("my_id").fadeOut();

ready




Pass in a "*" into the jQuery wrapper to get all the elements.

$(this) allows us to use jQuery methods to affect the current element.

Classing up your elements

In CSS, classes are used to group elements together and give them common style attributes.

Both CSS and jQuery use the "." to signify a class.

ID-entifying elements

An ID selector is used to identify a single, unique element on a page.


The problem caused by scripts is that they block parallel downloads in the browser.

Images from different servers can be downloaded more than two at a time, but once your browser encounters a <script> tag, it can no longer download multiple items in parallel. Having them at the bottom means it will help speed up your page load time.

Descendant Selectors




Wireup Pages