1 of 92

Javascript�Let’s get functional

Week 9

2 of 92

jQuery�But, in like, an easier way...

3 of 92

JavaScriptIs a coding language that connects to your HTML to add functionality.��People often refer to asking for some code in “vanilla javascript” to distinguish itself as having no added features or you know, flavours.

jQueryA tool written in javascript.

It is a “library” for javascript that you can pull items from to write javascript shorter without remembering as much.

Why jQuery

4 of 92

“jQuery is like learning how to cook first using a microwave. Sure, you can reheat and prepare quite a few meals using the microwave, but it’s not really “cooking”. You wouldn’t cook Thanksgiving dinner in the microwave, right?”��-Chris Castiglione

Why jQuery

5 of 92

“What the hell, why aren’t I learning JavaScript then if this is the equivalent to the microwaving of cooking”

Why jQuery

6 of 92

  1. End of the day, you are designers, not developers. High end knowledge is not required.��You are not training to be a chef, you just need to interact with the chef. Right now, we just need to learn what food even is.

Why jQuery

7 of 92

  • The learning curve for javaScript is high, you’d need more time to learn it and I think you all want a breather

Why jQuery

8 of 92

  • When you google stuff, you will get answers in jQuery and vanilla JavaScript. ��You can not use jQuery in your .js file without proper set up. However, you can use vanilla javascript in your .js jQuery file.

Why jQuery

9 of 92

“But I want to be a designer who also develops code”

Why jQuery

10 of 92

“But I want to be a designer who also develops code”

That is excellent. You will be in high demand in the job market and have a lot of versatility.

While developers aren’t using much jQuery anymore, it is an excellent starting point for beginners. At the end of the day, it’s still javascript, we are just writing in shortcuts.

The code concepts and language we will be learning are directly transferable; they just take longer to write in javaScript.

Why jQuery

11 of 92

“But why is it the microwave of cooking if everything is directly transferable to javaScript? Why don’t people use jQuery”

Why jQuery

12 of 92

“But why is it the microwave of cooking if everything is directly transferable to javaScript? Why don’t people use jQuery”

  1. It’s bloated and we’re all about speed�The library is very large and full of content. This results in large resources having to be downloaded for tasks that you can just do in plain vanilla javaScript that will render the functionality faster as well as just load the page faster.

Why jQuery

13 of 92

“But why is it the microwave of cooking if everything is directly transferable to javaScript? Why don’t people use jQuery”

  • Popular frameworks�Right now, the popular way to code is a with an external javaScript “framework” that does not use jQuery. Popular ones today are called React, Angular, and Vue. ��This is much higher level than where we’re at. If you are interested in being a developer/designer, it is definitely recommended to take some extra classes in these languages down the road or take tutorials on your free linkedIn learning while you have access to it.

Why jQuery

14 of 92

Preamble over�Let’s get functional w/ jQuery

Week 9

15 of 92

JavaScript

Setting up jQuery

16 of 92

Make another folder guys :)�Last new folder, I promise!

images

css

sass

js

index.html

Setting up jQuery

17 of 92

Make another folder guys :)�Last new folder, I promise!

images

css

sass

js

index.html

Setting up jQuery

18 of 92

images

css

sass

js

index.html

Setting up jQuery

19 of 92

Linking it

Unlike our CSS or Google fonts, we link our javascript right at the end of the page, just before the closing </body> tag.

This is the standard location for these files. Have the entire page load first before the javascript is read.

<script src="js/myscripts.js"></script>

</body>�</html>

Setting up jQuery

20 of 92

But with jQuery, that’s not all.

Setting up jQuery

21 of 92

We need to link/install our jQuery library

**** THIS MUST appear before the script tag you added with your own js file or it WILL NOT work ****

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script src="js/myscripts.js"></script>

</body>�</html>

Setting up jQuery

22 of 92

Some jQuery Concepts

23 of 92

Some javascript/jQuery Concepts

Variables

jQuery concepts: Variables

24 of 92

Variables

You can store content or data in a shortcut word called a variable.

�Simply define that variable’s word or name is in your file and put anything in it.

var cookies = 10;

jQuery concepts: Variables

25 of 92

var cookies = 10;

To define a new variable in your js file, the formula is �var variablename = value;

Declares we are defining a variable

Your custom variable name (use only letters). No spaces!

The value

jQuery concepts: Variables

26 of 92

var cookies = 10;

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)

jQuery concepts: Variables

27 of 92

var cookies = 10;

cookies = cookies-1;

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)

You can do math with variables that are defined as number based variables.

jQuery concepts: Variables

28 of 92

var cookies = 10;

cookies = 10-1;

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)

jQuery concepts: Variables

29 of 92

var cookies = 10;

var name = 'Jessica';

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)�- Text (string)

Text can be any length and multiple words just wrap it in between apostrophes.

jQuery concepts: Variables

30 of 92

var cookies = 10;

var name = 'Jessica';

var favQuote = 'Learning never exhausts the mind.';

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)�- Text (string)

jQuery concepts: Variables

31 of 92

var cookies = 10;

var name = 'Jessica';

var table = $('.table');

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)- Text (string)

- Objects in your HTML

We’ll talk about targeting HTML elements later

jQuery concepts: Variables

32 of 92

var cookies = 10;

var name = 'Jessica';

var table = $('.table');

var happy = true;

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)- Text (string)

- Objects in your HTML

- True or false (boolean)

Can either be set to true or false

jQuery concepts: Variables

33 of 92

var cookies = 10;

cookies = cookies-1;

To define a new variable in your js file, the formula is �var variablename = value;

Variables can store a few types of things:�- Numbers (integers or floats)- Text (string)

- Objects in your HTML

- True or false (boolean)

After a variable has been defined, you can simply call the variable in the document whenever you want.

jQuery concepts: Variables

34 of 92

Some javascript/jQuery Concepts

If statements (IFTTT)�If this, then that.

35 of 92

If statements

Jessica’s parents have given her very specific instructions.

If she gets above an 80% in her English class, she will get 2 cookies as a reward. If not, but she gets above a 65%, she’ll get 1 cookie as a reward. If she does any worse than that, she won’t get any cookies.

jQuery concepts: if statements

36 of 92

If Jessica gets above an 80:

She gets 2 cookies

Otherwise, if she gets above a 65:�She gets 1 cookie

If she doesn’t reach those criteria:�She gets nothing

jQuery concepts: if statements

37 of 92

var grade;

var cookieStash = 0;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

If Jessica gets above an 80:

She gets 2 cookies

Otherwise, if she gets above a 65:�She gets 1 cookie

If she doesn’t reach those criteria:�She gets nothing

38 of 92

var grade;

var cookieStash = 0;

grade = 70;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

39 of 92

var grade;

var cookieStash = 0;

grade = 70;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

cookieStash = cookieStash+1;

1 cookie

40 of 92

var grade;

var cookieStash = 0;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

41 of 92

var grade;

var cookieStash = 0;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

cookieStash = cookieStash+0;

0 cookies

42 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

43 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade > 80) {

cookieStash = cookieStash+2;

}

else if(grade > 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

cookieStash = cookieStash+1;

1 cookie

44 of 92

If statement comparative symbols

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

== Exactly equal to

!= Does not equal

jQuery concepts: if statements

45 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

46 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

jQuery concepts: if statements

cookieStash = cookieStash+2;

2 cookies

47 of 92

What’s the point of “else if”, why don’t I start another if?

jQuery concepts: if statements

48 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade >= 80) {

cookieStash = cookieStash+2;

}

if(grade >= 65) {

cookieStash = cookieStash+1;

}

jQuery concepts: if statements

49 of 92

var grade;

var cookieStash = 0;

grade = 80;

if(grade >= 80) {

cookieStash = cookieStash+2;

}

if(grade >= 65) {

cookieStash = cookieStash+1;

}

jQuery concepts: if statements

Both statements will be read as true. Resulting in more cookies than intended

cookieStash = cookieStash+2;

cookieStash = cookieStash+1;

3 cookies

50 of 92

if(grade >= 80) {

cookieStash = cookieStash+2;

}

if(grade >= 65) {

cookieStash = cookieStash+1;

}

jQuery concepts: if statements

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

51 of 92

if(grade >= 80) {

cookieStash = cookieStash+2;

}

if(grade >= 65) {

cookieStash = cookieStash+1;

}

jQuery concepts: if statements

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

var grade = 80;

52 of 92

jQuery concepts: if statements

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

var grade = 80;

53 of 92

jQuery concepts: if statements

if(grade >= 80) {

cookieStash = cookieStash+2;

}

else if(grade >= 65) {

cookieStash = cookieStash+1;

}

else {

cookieStash = cookieStash+0;

}

var grade = 70;

54 of 92

How complicated can I get with an �if statement?

jQuery concepts: if statements

55 of 92

How complicated can I get with an �if statement?

jQuery concepts: if statements

Incredibly complicated. If you know an if statement, you can do anything.

56 of 92

How complicated can I get with an �if statement?

jQuery concepts: if statements

Incredibly complicated. If you know an if statement, you can do anything.

if( understandIfStatements == true) {

doAnything = true;

}

else {

doAnything = false;

}

57 of 92

Jessica’s parents have found great success in her English mark improvements from their cookie agreement, so they have decided to expand it.

If she gets above an 80% in her English and Math class, she will get 4 cookies. If she only gets above an 80% in either English or Math, she’ll get 2 cookies. Otherwise, they will take a cookie away from her stash.

jQuery concepts: if statements

58 of 92

jQuery concepts: if statements

var englishGrade;

var mathGrade;

var cookieStash = 6;

if(englishGrade>=80 && mathGrade>=80) {

cookieStash = cookieStash+4;

}

else if(englishGrade>=80 || mathGrade>=80) {

cookieStash = cookieStash+2;

}

else {

cookieStash = cookieStash-1;

}

59 of 92

If statement operative symbols

&& And

|| Or

jQuery concepts: if statements

60 of 92

jQuery concepts: if statements

var englishGrade;

var mathGrade;

var cookieStash = 6;

if((englishGrade>=80) && (mathGrade>=80)) {

cookieStash = cookieStash+4;

}

else if((englishGrade>=80) || (mathGrade>=80)) {

cookieStash = cookieStash+2;

}

else {

cookieStash = cookieStash-1;

}

61 of 92

You can use brackets to get really intense with if statement conditions

jQuery concepts: if statements

62 of 92

jQuery concepts: if statements

if((englishGrade>=80) && (mathGrade>=80)) {

cookieStash = cookieStash+4;

}

Jessica’s parents agreed that even if she didn’t get above 80 in english and math, that if she did maintain an average of above 90 then clearly she still deserved a reward.

if((englishGrade>=80 && mathGrade>=80) || gradeAverage>=90) {

cookieStash = cookieStash+4;

}

63 of 92

jQuery concepts: if statements

Think of these statements regarding how they are read with BEDMAS in mind. Some old school math :)

if((englishGrade>=80 && mathGrade>=80) || gradeAverage>=90) {

cookieStash = cookieStash+4;

}

64 of 92

You can also nest if statements inside each other if you need to

jQuery concepts: if statements

65 of 92

jQuery concepts: if statements

if(englishGrade>=80 && mathGrade>=80)) {

if(artGrade >= 70) {

cookieStash = cookieStash+6;

}

else {

cookieStash = cookieStash+4;

}

}

If Jessica gets above an 80 in english and math, she’ll get 4 cookies. But if she also gets above a 70 ontop of that, she’ll get 6 cookies instead of 4.

66 of 92

jQuery

Targeting HTML items

67 of 92

Target items in your HTML following this template:

$('targeted-item');

Start with a dollar sign and a pair of brackets. Inside the brackets, name the element in between apostrophes. Elements are targeted just like in CSS:

jQuery: targeting HTML items

<p>Lorem ipsum dolor sit amet.</p>

$('p');

$('.large');

$('#special-jump-link');

<div class="large"></div>

<h2 id="special-jump-link"></h2>

68 of 92

Target an HTML item’s direct parent or direct child element with .parent() and .children()

$('.large').parent('.container');

jQuery: targeting HTML items

$('.large').children('.button');

<div class="container">

<div class="large"></div>

</div>

<div class="large">

<a class="button"></a>

</div>

69 of 92

How the logic works: everything references whatever appears before it that is attached by a period.

$('.large').children('.button');

jQuery: targeting HTML items

Target elements with a class name of ‘large’. Then change my target to any elements that are inside my previous target (‘large’), that have a class name of ‘button’.

70 of 92

Target HTML items that appear somewhere inside your previous target, but not requiring it to be a direct child, with .find()

$('.large').find('.button');

jQuery: targeting HTML items

<div class="large">

<div class="nav">

<a class="button"></a>

</div>

</div>

71 of 92

Target the first HTML item that is containing your previous target, but not requiring it to be a direct parent, with .closest()

$('.large').closest('.container');

jQuery: targeting HTML items

<div class="container">

<div class="row">

<div class="col-xs-12">

<div class="large">

</div>

</div>

</div>

</div>

72 of 92

jQuery

Some quick shortcut things you can do functionally.

73 of 92

Hide something with .hide();

$('.fancy-disappearing-box').hide();

jQuery: functionality

74 of 92

Show something with .show();

$('.fancy-disappearing-box').show();

jQuery: functionality

75 of 92

Fade in or out an object to have it appear or disappear with some jazz

$('.fancy-disappearing-box').fadeIn();

$('.fancy-disappearing-box').fadeOut();

jQuery: functionality

76 of 92

jQuery

Manipulating class names on HTML items to change their CSS.

77 of 92

You can add or remove class names from an HTML element live.

$('.slider').addClass('open');

$('.slider').removeClass('open');

jQuery: functionality

78 of 92

.toggleClass() will simply add or remove a specified class name depending on whether it has it or not

$('.slider').toggleClass('open');

jQuery: functionality

79 of 92

javascript/jQuery

Functions

80 of 92

Functions can contain a set of commands within them, that when called, will run through.

function openSlider() {

$('.slider').toggleClass('open');

$('.slider').css('left', '0px');

}

jQuery: functions

81 of 92

Functions can contain a set of commands within them, that when called, will run through.

function closeSlider() {

$('.slider').toggleClass('open');

$('.slider').css('left', '-500px');

}

jQuery: functions

Name functions whatever you want! �(only letters, no spaces, case sensitive)

82 of 92

jQuery

Click events

83 of 92

To run jQuery commands when something is clicked, it is written like this, creating an unnamed click function:

$('.slider-button').click(function() {

openSlider();

});

jQuery: clicking something

84 of 92

$('.slider-button').click(function() {

$(this).hide();

});

jQuery: clicking something

You can use $(this) to refer to an object that has started a function (e.g., a click event) —it will not by default pass to another function

85 of 92

Video tutorial examples:

jQuery: make a bar slide in from the left

Creating a popup: �https://youtu.be/xFPwd7dKxwI

Explaining purpose of $(this): �https://youtu.be/qyFCVYrjZgs

Creating a slider: �https://youtu.be/9ApOaY49ExQ

86 of 92

Detecting where a user is scrolled in the page to and do something

jQuery: make a bar slide in from the left

87 of 92

Scroll event: pixel value

$(window).scroll(function () {

var scrollPos = $(window).scrollTop();

if (scrollPos > 100) {

$('.header').addClass('active');

} else {

$('.header').removeClass('active');

}

});

jQuery: scroll events

If you scroll past 100px, the header will have the active class added to it

88 of 92

$(window).scroll(function () {

var scrollPos = $(window).scrollTop();

if (scrollPos > $('.intro').offset().top) {

$('.header').addClass('active');

} else {

$('.header').removeClass('active');

}

});

jQuery: scroll events

Scroll event: object

If you scroll past the top of the intro of the page (.intro), the header will have the active class added to it

89 of 92

Recognizing javaScript

jQuery: recognizing the differences from javaScript

90 of 92

jQuery and JavaScript comparison

jQuery: recognizing the differences from javaScript

$('p');

$('.large');

$('#special-jump-link');

var par = document.querySelectorAll('p');

document.querySelectorAll('.large');

document.querySelector('#special-jump-link');

91 of 92

jQuery and JavaScript comparison

jQuery: recognizing the differences from javaScript

$('.slider').addClass('open');

document.querySelectorAll('.slider').forEach(function(aSlider) {

aSlider.classList.add('open');

});

92 of 92

Micro-Assignment