jquery essentials v2 - marc grabanski - web...

115
jQuery Essentials by Marc Grabanski v2

Upload: vanquynh

Post on 16-Mar-2018

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Essentialsby Marc Grabanski

v2

Page 2: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

We needed a hero to get these guys in line

Page 3: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery rescues us by workingthe same in all browsers!

Page 4: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Easier to write jQuery than pure JavaScript

Page 5: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

divs = document.getElementByTagName(‘div’);

for (i = 0; i < divs.length; i++) {divs[i].style.display = ‘none’;

}

Hide divs with pure JavaScript

Page 6: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

divs = document.getElementByTagName(‘div’);

for (i = 0; i < divs.length; i++) {divs[i].style.display = ‘none’;

}

Hide divs with pure JavaScript

$(“div”).hide();

Hide divs with jQuery

Page 7: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

HTML is tied to JavaScript

Page 8: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Philosophy

Page 9: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

#1. Find some HTML

jQuery Philosophy

Page 10: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

#1. Find some HTML

#2. Do something to it

jQuery Philosophy

Page 11: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“div”)

Find

Page 12: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“div”)

Find let’s find some elements

Page 13: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Give $() a selector

Page 14: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Give $() a selector

$(“#myId”)

Page 15: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Give $() a selector

$(“#myId”) $(“.myClass”)

Page 16: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Give $() a selector

$(“#myId”) $(“.myClass”) $(“table”)

Page 17: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Selector Examples

$(“#content”) get element with id content

Page 18: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Selector Examples

$(“#content”) get element with id content

$(“li:first”) get first list item

Page 19: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Selector Examples

$(“#content”) get element with id content

$(“li:first”) get first list item

$(“tr:odd”) get odd numbered table rows

Page 20: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Selector Examples

$(“#content”) get element with id content

$(“li:first”) get first list item

$(“tr:odd”) get odd numbered table rows

$(“a[target=_blank]”)get all links who’s target is “_blank”

Page 21: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Selector Examples

$(“#content”) get element with id content

$(“li:first”) get first list item

$(“tr:odd”) get odd numbered table rows

$(“form[id^=step]”)get all forms who’s id starts with “step”

$(“a[target=_blank]”)get all links who’s target is “_blank”

Page 22: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

You can also string selectors together

Page 23: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

You can also string selectors together

$(“#myId, .myClass, table”)

Page 24: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“div”)

Find

Page 25: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Do

.addClass(“redbox”);$(“div”)

Find

Page 26: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery API SpiceTwo things that make the API HOT

Page 27: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“div”).addClass(“redbox”)

Chain Methods

Page 28: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“div”).addClass(“redbox”)

Chain Methods

.fadeOut();

Page 29: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html();

One Method, Many Uses

Page 30: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html();

One Method, Many Uses

$(...).html(“<p>hello</p>”);

Page 31: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html();

One Method, Many Uses

$(...).html(“<p>hello</p>”);

$(...).html(function(i){

return “<p>hello “ + i + “</p>”;

});

Page 32: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

Page 33: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

Page 34: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

•Attributescss(), attr(), html(), val(), addClass()

Page 35: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

•Attributescss(), attr(), html(), val(), addClass()

•Eventsbind(), trigger(), unbind(), live(), click()

Page 36: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

•Attributescss(), attr(), html(), val(), addClass()

•Eventsbind(), trigger(), unbind(), live(), click()

•Effectsshow(), fadeOut(), toggle(), animate()

Page 37: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

•Attributescss(), attr(), html(), val(), addClass()

•Traversingfind(), is(), prevAll(), next(), hasClass()

•Eventsbind(), trigger(), unbind(), live(), click()

•Effectsshow(), fadeOut(), toggle(), animate()

Page 38: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Methods

•Moving Elements: append(), appendTo(), before(), after(),

•Attributescss(), attr(), html(), val(), addClass()

•Traversingfind(), is(), prevAll(), next(), hasClass()

•Eventsbind(), trigger(), unbind(), live(), click()

•Ajaxget(), getJSON(), post(), ajax(), load()

•Effectsshow(), fadeOut(), toggle(), animate()

Page 39: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Factory Method $()

You can also pass $() a functionto run the function after the page loads.

Page 40: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Factory Method $()

You can also pass $() a functionto run the function after the page loads.

$(function(){

});

Page 41: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Factory Method $()

You can also pass $() a functionto run the function after the page loads.

$(function(){

});code here will execute after DOM is ready

Page 42: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Factory Method $()

You can also pass $() a functionto run the function after the page loads.

$(function(){

});

Note: This is essentially the same as..$(document).ready(function(){ });

code here will execute after DOM is ready

Page 43: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery Factory Method $()

You can also pass $() a functionto run the function after the page loads.

$(function(){

});

Note: This is essentially the same as..$(document).ready(function(){ });

code here will execute after DOM is ready

..you will see this in tutorials around the net

Page 44: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“#foo”)

Get element with ID foo and add some HTML.

<html><body>

<div>jQuery</div><div id=”foo”>example</div>

</body></html>

Moving Elements Examples

Page 45: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“#foo”)

Get element with ID foo and add some HTML.

<html><body>

<div>jQuery</div><div id=”foo”>example</div>

</body></html>

Moving Elements Examples

.append(“<p>test</p>”);

Page 46: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“#foo”)

Get element with ID foo and add some HTML.

<html><body>

<div>jQuery</div><div id=”foo”>example<p>test</p></div>

</body></html>

Moving Elements Examples

.append(“<p>test</p>”);

Page 47: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

<html><body>

<div>jQuery<p>moving</p> <p>paragraphs</p>

</div><div id=”foo”>example</div>

</body></html>

$(“p”)

Move paragraphs to element with id “foo”

Moving Elements Examples

Page 48: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

<html><body>

<div>jQuery<p>moving</p> <p>paragraphs</p>

</div><div id=”foo”>example</div>

</body></html>

$(“p”)

Move paragraphs to element with id “foo”

Moving Elements Examples

.appendTo(“#foo”);

Page 49: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“p”)

Move paragraphs to element with id “foo”

<html><body>

<div>jQuery</div><div id=”foo”>example

<p>moving</p> <p>paragraphs</p>

</div></body>

</html>

Moving Elements Examples

.appendTo(“#foo”);

Page 50: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

Page 51: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

Page 52: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

Page 53: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

Page 54: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.css(“top”)

Page 55: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.width()

.css(“top”)

Page 56: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

Set

.attr(‘id’, ‘foo’)

.width()

.css(“top”)

Page 57: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.html(“<p>hi</p>”)

Set

.attr(‘id’, ‘foo’)

.width()

.css(“top”)

Page 58: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.html(“<p>hi</p>”)

Set

.attr(‘id’, ‘foo’)

.val(“new val”)

.width()

.css(“top”)

Page 59: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.html(“<p>hi</p>”)

Set

.attr(‘id’, ‘foo’)

.val(“new val”)

.width()

.css(“top”) .css(“top”, “80px”)

Page 60: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

.attr(‘id’)

Get

.html()

.val()

.html(“<p>hi</p>”)

Set

.attr(‘id’, ‘foo’)

.val(“new val”)

.width() .width(60)

.css(“top”) .css(“top”, “80px”)

Page 61: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

Page 62: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).css(“border”, “1px solid black”); Set border to 1px black

Attributes

Page 63: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).css(“border”, “1px solid black”); Set border to 1px black

Set various css properties.$(...).css({

“background”: “yellow”, “height”: “400px”

});

Attributes

Page 64: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).css(“border”, “1px solid black”); Set border to 1px black

Set various css properties.$(...).css({

“background”: “yellow”, “height”: “400px”

});

$(“a”).attr(“href”, “http://google.com”);Set all link’s href attribute to google.com

Attributes

Page 65: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Attributes

Page 66: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html(“<p>I’m new</p>”); Replace HTML with a new paragraph.

Attributes

Page 67: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html(“<p>I’m new</p>”); Replace HTML with a new paragraph.

Attributes

<div>whatever</div> turns into<div><p>I’m new</p></div>

Page 68: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html(“<p>I’m new</p>”); Replace HTML with a new paragraph.

Set checkboxes attribute “checked” to checked.$(“:checkbox”).attr(“checked”,”checked”);

Attributes

<div>whatever</div> turns into<div><p>I’m new</p></div>

Page 69: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html(“<p>I’m new</p>”); Replace HTML with a new paragraph.

Set checkboxes attribute “checked” to checked.$(“:checkbox”).attr(“checked”,”checked”);

$(...).val(“3”);Set input value to 3.

Attributes

<div>whatever</div> turns into<div><p>I’m new</p></div>

Page 70: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).html(“<p>I’m new</p>”); Replace HTML with a new paragraph.

Set checkboxes attribute “checked” to checked.$(“:checkbox”).attr(“checked”,”checked”);

$(...).val(“3”);Set input value to 3.

$(...).val();Get input value.

Attributes

<div>whatever</div> turns into<div><p>I’m new</p></div>

Page 71: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Events Examples

Page 72: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Events

Page 73: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“button”).click(function(){something();

});

When a button is clicked, do something.

Events

Page 74: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“button”).click(function(){something();

});

When a button is clicked, do something.

Setup a custom event and trigger it.$(“button“).bind(“expand”, function(){

something();}); $(“button:first“).trigger(“expand”);

Events

Page 75: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“button”).click(function(){something();

});

When a button is clicked, do something.

Setup a custom event and trigger it.$(“button“).bind(“expand”, function(){

something();}); $(“button:first“).trigger(“expand”);

Events

$(“button“).unbind(“expand”);Unbind custom event.

Page 76: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Event Delegation

Page 77: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“button”).live(‘click’, function(){something();

});

Event Delegation

Attach events to document

Page 78: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“button”).live(‘click’, function(){something();

});

Event Delegation

Attach events to document

Attach event delegation to elements

$(“form“).delegate(“button”, ”click”, function(){something();

});

Page 79: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Effects / Animation Examples

Page 80: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Animation / Effects

Types of Effects

Page 81: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Animation / Effects

#1. Hide and Show

Types of Effects

Page 82: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Animation / Effects

#2. Fade In and Out

#1. Hide and Show

Types of Effects

Page 83: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Animation / Effects

#2. Fade In and Out

#1. Hide and Show

#3. Slide Up and Down

Types of Effects

Page 84: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Animation / Effects

Page 85: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).click(function(){$(“div:first”).slideToggle();

});

With each click, slide up / slide down a div.

Animation / Effects

Page 86: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).click(function(){$(“div:first”).slideToggle();

});

With each click, slide up / slide down a div.

Animate elements to 300px wide in .5 seconds.$(...).animate({ “width”: “300px” }, 500);

Animation / Effects

Page 87: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).click(function(){$(“div:first”).slideToggle();

});

With each click, slide up / slide down a div.

Animate elements to 300px wide in .5 seconds.$(...).animate({ “width”: “300px” }, 500);

$(...).fadeTo(500, 0.3);

Take focus off elements by fading them to 30% opacity in .5 seconds

Animation / Effects

Page 88: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Traversing Examples

Page 89: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“#myCell”)

Get previous table cells to #myCell.

<html><body>

<table><tr><td></td><td></td><td id=”myCell”></td><td></td>

</tr></table></body>

</html>

Traversing Examples

Page 90: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

<html><body>

<table><tr><td></td><td></td><td id=”myCell”></td><td></td>

</tr></table></body>

</html>

$(“#myCell”)

Get previous table cells to #myCell.

Traversing Examples

.prevAll()

Page 91: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“#myCell”)

Get previous table cells to #myCell.

Traversing Examples

.prevAll()

<html><body>

<table><tr><td></td><td></td><td id=”myCell”></td><td></td>

</tr></table></body>

</html>

.andSelf();

Page 92: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

<html><body>

<table></table><div>

<p>foo</p><span>bar</span>

</div></body>

</html>

$(“table”)

Move paragraphs to element with id “foo”

Traversing Examples

Page 93: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

<html><body>

<table></table><div>

<p>foo</p><span>bar</span>

</div></body>

</html>

$(“table”)

Move paragraphs to element with id “foo”

Traversing Examples

.next()

Page 94: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“table”)

Move paragraphs to element with id “foo”

<html><body>

<table></table><div>

<p>foo</p><span>bar</span>

</div></body>

</html>

Traversing Examples

.next()

Page 95: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(“table”)

Move paragraphs to element with id “foo”

Traversing Examples

.next().find(“p”);

<html><body>

<table></table><div>

<p>foo</p><span>bar</span>

</div></body>

</html>

Page 96: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Ajax Examples

Page 97: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Ajax Examples

Page 98: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).get(“tag.php”, { “bar”: “baz” });

Post data, “bar” equals “baz” to tag.php using get.

Ajax Examples

Page 99: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$(...).get(“tag.php”, { “bar”: “baz” });

Post data, “bar” equals “baz” to tag.php using get.

Post data, “foo” equals “bar” to send.php, thenalert the response.

$.post(“send.php”, { foo: ”bar” }, function(response){

alert(response);});

Ajax Examples

Page 100: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Extending jQuery

Page 101: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$.fn.myPlugin = function(){return this.each(function(){

$(this).html(“you used myPlugin!”);});

});

<html><body>

<div></div><div></div>

</body></html>

Plugin Example

Page 102: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$.fn.myPlugin = function(){return this.each(function(){

$(this).html(“you used myPlugin!”);});

});

$(“div”).myPlugin();<html>

<body><div></div><div></div>

</body></html>

Plugin Example

Page 103: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

$.fn.myPlugin = function(){return this.each(function(){

$(this).html(“you used myPlugin!”);});

});

<html><body>

<div>you used myPlugin!</div><div>you used myPlugin!</div>

</body></html>

$(“div”).myPlugin();

Plugin Example

Page 104: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Wait, There’s More!

Page 105: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery isn’t only about simpler code

Page 106: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery isn’t only about simpler codeand being more productive

Page 107: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery isn’t only about simpler codeand being more productive

It is also about..

Page 108: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

jQuery isn’t only about simpler codeand being more productive

It is also about..great community

support tutorials

plugins

open (free) license

test coverage books

speed

light weight code

Page 110: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Usage Across Top 10,000 Sites

http://trends.builtwith.com/javascript

Page 111: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Plugins

jQuery has hundreds of plugins athttp://plugins.jquery.com/

jQuery UI

Set of official user interface components at:http://jqueryui.com

Page 112: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

http://forum.jquery.com

Support

http://docs.jquery.com/Discussion

jQuery general discussion mailing list

jQuery discussion docs page

jQuery IRC room#jquery on FreeNode.net

Page 113: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Books

http://www.amazon.com/gp/product/1847196705?ie=UTF8&tag=jacofalltrawe-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1847196705

http://www.amazon.com/gp/product/1933988355?ie=UTF8&tag=jacofalltrawe-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1933988355

Learning jQuery 1.3by Karl Swedberg

jQuery in ActionYahuda Katz

Page 115: jQuery Essentials v2 - Marc Grabanski - Web …marcgrabanski.com/presentations/jquery-essentials.pdfSelector Examples $(Ò #content Ó) get element with id content $(Ò li:firstÓ)

Thank you!

Marc Grabanski: http://marcgrabanski.com

Twitter: @1Marc