A Smarter Way To Learn JQuery - Mark Myers
A Smarter Way To Learn JQuery - Mark Myers
To Learn jQuery
Learn it faster. Remember it longer.
Mark Myers
Copyright 2015 Mark Myers
All rights reserved, including the right to reproduce this book, or any portions of it, in any
form.
1.1
http://www.ASmarterWayToLearn.com
Contents
2 Add a class
3 Select by class
4 Select by id
14 this
18 Remove things
19 Toggle a class
20 Replace things
21 Another way to replace
22 Variables in jQuery
23 Variable names legal and illegal
24 Personalize a heading
34 Remove an attribute
38 Animate an expansion
45 Delay
46 Stop a slide or animation
61 Select by next
62 The DOM
63 The DOM: Parents and children
70 Not
71 Highlight the active text field
72 each
73 Get information about an event
Study, practice, then rest. If youre intent on mastering the fundamentals of jQuery,
as opposed to just getting a feel for it, work with this book and the online exercises in
a 15-to-30-minute session, then take a break. Study a chapter for 5 to 10 minutes.
Immediately go to the online links given at the end of each chapter and code for 10 to
20 minutes, practicing the lesson until youve coded everything correctly. Then take a
walk.
Dont wear yourself out. You learn best when youre fresh. If you try to cover too
much in one day, your learning will go downhill. Most people find they can
comfortably cover one to three chapters a day. Your experience may vary.
If you find some of the repetition tiresome, skip exercises. I wrote the exercises for
people like me, who need a lot of repetition. If youre a fast learner or a learner with
some coding experience, theres no reason to burden yourself. Click the Skip
Exercise and Get Credit button to jump ahead. Skip whole sets of exercises if you
dont need them. Practice as much as you need to, but no more.
If you struggle with some exercises, you know youre really learning. An
interesting feature of your brain is that the harder it is for you to retrieve a piece of
information, the better you remember it next time. So its actually good news if you
have to struggle to recall something from the book. Dont be afraid to repeat a set of
exercises. And consider repeating exercises after letting a few weeks go by. If you do
this, youll be using spaced repetition, a power-learning technique that provides even
more long-term retention.
Do the coding exercises on a physical keyboard. A mobile device can be ideal for
reading, but its no way to code. Very, very few Web developers would attempt to do
their work on a phone. The same thing goes for learning to code. Theoretically, most
of the interactive exercises could be done on a mobile device. But the idea seems so
perverse that Ive disabled online practice on tablets, readers, and phones. (It also
simplified my own coding work.)
If you have an authority problem, try to get over it. When you start doing the
exercises, youll find that I can be a pain about insisting that you get every little detail
right. For example, if you omit a semicolon, the program monitoring your work will
tell you the code isnt correct, even though it might run. Learning to write code with
precision helps you learn to pay close attention to details, a fundamental requirement
for coding in any language.
Subscribe, temporarily, to my formatting biases. Current code formatting is like
seventeenth-century spelling. Everyone does it his own way. There are no universally
accepted standards. But in the exercises, the algorithms that check your work need
standards. They cant grant you the latitude that a human teacher could, because, lets
face it, algorithms arent that bright. So Ive had to settle on certain conventions. All
of the conventions I teach are embraced by a large segment of the coding community,
so youll be in good company. But that doesnt mean youll be married to my
formatting biases forever. When you begin coding projects, youll soon develop your
own opinions or join an organization that has a stylebook. Until then, Ill ask you to
make your code look like my code.
The language youre learning here
jQuery is a JavaScript library. That is, its a collection of JavaScript routines that do
all kinds of things for you without requiring you to write the underlying JavaScript code.
Its a kind of shorthand. Its a robot that codes JavaScript.
Like JavaScript, jQuery must always be linked to by the HTML file thats creating
the webpage. Youll learn how to write this link in Chapter 27.
Its good to have some knowledge of JavaScript when youre learning jQuery, but
plenty of people dont. You will need to understand the fundamentals of HTML and CSS,
though, in order to understand jQuery. If HTML or CSS arent in your toolkit, it might be
a good idea to put this book aside for a few weeks and start with my A Smarter Way to
Learn HTML & CSS.
1
Target and action
Lets say you want to give the user the option to convert your webpage to text-only.
The user clicks a button, and all the images on the page vanish.
In a later chapter Ill tell you about the code for the button.
For now, take a look at the jQuery code that hides all the images:
$(img).hide();
The statement has four parts.
Things to notice:
The statement begins with a dollar sign. This alerts the program that jQuery follows.
Its like saying, The following statement is in French: Bonjour, Madame. The target
the thing or things to be acted onbegins with the dollar sign. Then theres a pair
of parentheses. Within the parentheses is a CSS selector (or possibly more than one)
enclosed in quotation marks. In the example, the CSS selector is img. No particular
image has been selected, so the target comprises all the images on the page.
The jQuery selector comprises the dollar sign, the parentheses, the pair of quotation
marks and the CSS selector, img.
The jQuery selector tells jQuery which element or elements on the page to select for
the action that follows. More examples: $(p) selects paragraphs, $(h2)
selects next-to-largest headings, $(div)selects divs, $(div, p) selects
both divs and paragraphs, $(ul)selects unordered lists. In each case, the heart of
the jQuery selector is a CSS selector: p, h2, div, ul.
A dot separates the selector from the action.
The action. Technically, its known as a method. When I write hide(), Im asking
jQuery to hide the target. Note that the method ends with a pair of parentheses.
A semicolon marks the end of the statement. Its like a period that marks the end of a
sentence in a spoken language.
Alternatives to be aware of
Instead of writing
$(img).hide();
you can write
jQuery(img).hide();
or you can write
window.$(img).hide();
or
window.jQuery(img).hide();
In this book and the online exercises that accompany it, Ill stick with the dollar sign.
I wont test you on the alternatives.
But notice a difference. In the statement you learned in the last chapter, the method
name was followed by empty parentheses: hide(). In this statement, the parentheses
enclose the name of the class to be added. Note that the class name is in quotation marks:
addClass(big).
addClass means what it says: add a classnot replace a class. So if an
element has already been assigned one or more classes, the new class will be in addition to
those classes.
You can add more than one class at a time. The following code adds the three classes
big, bright, and heavy to all h2 headings:
$(h2).addClass(big bright heavy);
Note that all three class names are enclosed in a single set of parentheses. Theyre
separated only by a space. There are no commas.
Once again, the heart of the jQuery selector is a CSS selector : #greeting.
Again, a dot separates the selector and the method.
The method is named text. It replaces any text in a paragraph whose id is
#greeting.
As in all methods, parentheses follow the method.
Within the parentheses, the text to be inserted is in quotation marks.
Instead of specifying only the id, you can include the element type that has the id.
$(p#greeting).text(Hello World!);
Since an id can apply to only one element on the page, specifying the element isnt
necessary, but I like to do it to make things a little clearer for human readers.
Suppose you want to include HTML tags within the paragraph. If you write the
following code, expecting jQuery will display the text in bold
$(#greeting).text(<b>Hello World!</b>);
jQuery will disappoint you by displaying
<b>Hello World!</b>
When you want jQuery to interpret HTML tags as HTML tags, instead of using the
text() method, you write
$(#greeting).html(<b>Hello World!</b>);
Now it displays
Hello World!
var is the keyword you use to start any variable declaration. It says: I declare the
following to be a variable.
addressEntered = says to store the result of the jQuery action in the JavaScript
variable addressEntered. For example, if the user has entered [email protected]
in the field whose id is e_add, addressEntered now has the value
[email protected]. With this data stored in the variable, you can use JavaScript to test
it for validity, as I show how to do in Chapter 85 of my
A Smarter Way to Learn JavaScript. If your JavaScript determines that the address
looks wrong, the script can alert the user and ask him to re-enter it.
By now you know what $(input#e_add) means: Select the element with the
id of e_add. In this case, the element with that id is the email address field.
The dot separates the selector from the method.
val() is the method that gets the valuewhatever the user has entered in the
field. The value in the field is stored in the variable addressEntered.
The semicolon ends the statement.
As usual, the statement begins with a dollar sign followed by the rest of the selector.
In this case, the target is the button with an id of b1. You could shorten the selector
by omitting button, since the only item with that particular id is a button:
$(button#b1). I prefer to include button, to make things clear for humans.
Next comes a dot, and then on. The word on means when the following event
occurs.
Then theres an opening parenthesis, followed by the name of the event, in quotation
marks: (click
An alternative to be aware of
Instead of writing
$(button#b1).on(click, function() {
you can shorten it by writing
$(button#b1).click (function() {
Many experts prefer the long version because it requires less processing. Thats the
version Ill stick with in this book and the exercises.
h3 headings
paragraphs of the class not_so_important
a div with the id fine_print.
all list items of the class secondary that are in a list with an id of things_to_check
Note that all four CSS selectors are enclosed within a single set of quotation marks
and parentheses. Theyre separated by a comma followed by a space.
abstract final
alert finally public
as float return
boolean for short
break function static
byte goto super)
case if switch
catch implements synchronized
char import this
class in throw
continue instanceof throws
const int transient
debugger interface true
default is try
delete long typeof
do namespace use
double native var
else new void
enum null volatile
export package while
extends private with
false protected
Though a variable name cant be any of JavaScripts keywords, it can contain
keywords. For example, userAlert and myVar are legal.
I teach the camelCase naming convention. Why camelCase? Because there is a
hump or two (or three or more) in the middle if the name is formed by more than one
word. A camelCase name begins in lower case. If theres more than one word in the
name, each subsequent word gets an initial cap, creating a hump. If you form a
variable name with only one word, like response, theres no hump. Its a camel thats
out of food. Please adopt the camelCase convention. Itll make your variable names
more readable, and youll be less likely to get variable names mixed up.
Examples:
userResponse
userResponseTime
userResponseTimeLimit
response
Make your variable names descriptive, so its easier to figure out what your code
means when you or someone else comes back to it three weeks or a year from now.
Generally, userName is better than x, and faveBreed is better than favBrd,
though the shorter names are perfectly legal. You do have to balance readability with
conciseness, though. bestSupportingActressInADramaOrComedy is a
model of clarity, but may be too much for most of us to type or read. Id shorten it.
lettuce
celery
carrots
lettuce
celery
carrots
rutabagas
As you can see from the example, you can append HTML markup as well as text. In
the example above, <li> and </li> are included in the append. Its necessary to include
these tags if you want to include rutabagas as a list item, rather than just text. If, omitting
the tags, you write
$(ul#vegetables).append(rutabagas);
you get this:
lettuce
celery
carrots
rutabagas
Of course, you can append nothing but text when you want to. Suppose you want to
add some text to the first list item, which has an id of i1. Heres the code:
$(li#i1).append(, Romaine);
The page displays this:
lettuce, Romaine
celery
carrots
Paragraph 1
Paragraph 2
Paragraph 3
The following code places an h2 heading at the beginning of the div, above the first
paragraph
$(div#d1).prepend(<h2>These are three important
paragraphs.</h2>);
So now this displays on the page:
Paragraph 1
Paragraph 2
Paragraph 3
You can also use the prepend() method to add text onlywithout HTML markup
to the beginning of something. For example, you could add some text to the beginning
of the second paragraph:
$(div#p2).prepend(Be sure to read this important
information carefully: );
This displays:
Paragraph 1
Paragraph 3
Apples
Oranges
Bananas
You want to add a new list item, Grapefruit, after Oranges. You write:
$(li#i2).after(<li>Grapefruit</li>);
The result:
Apples
Oranges
Grapefruit
Bananas
Things to notice:
The statement begins by selecting the element after which the new item will be
inserted: $(li#i2)
Next comes the dot and the method: .after(
Finally, the content that is to be inserted. Its enclosed in quotation marks, with a
closing parenthesis and semicolon: <li>Grapefruit</li>);
Find a demo for this chapter at:
http://jsfiddle.net/ASmarterWayToLearn/f7tnw6bb/
Find the interactive coding exercises for this chapter at:
http://www.ASmarterWayToLearn.com/jquery/31.html
32
Insert something before something else
You have some images of cute animals. Youve assigned all of the images the class
cute. Heres one of them:
You want to insert an h3 heading above all the images of this class. This is the code:
$(img.cute).before(<h3>Is this cute or what?</h3>);
The result:
The original, truncated height is 4em. The statement above tells the program to let the
user see the paragraph expand from the bottom down to a height of 10em.
The name of the method is animate.
Like all methods, the method is followed by parentheses, but
Within the parentheses, the details of the animation are enclosed in curly brackets:
animate({height: 10em})
The attribute isnt in quotation marks and is followed by a colon:
animate({height: 10em})
The value of the attribute is in quotation marks:
animate({height: 10em})
Heres a statement that expands both width and height, using pixels rather than ems
as the values:
$(a#show_more).on(click, function() {
$(p#ex_1).animate({width: 250px, height: 300px});
});
Note that a comma separates the two specifications.
Normally, youd delay flying in the picture for at least a few seconds after the user
arrives at the page. But we wont deal with that just yet. Heres the code for the effect:
$(div#daily).animate({left: 15%});
The code above animates the div so it moves from wherever it starts outin this
case out of view on the leftto the position specified within the curly bracketsin this
case 15% to the right of the left edge of the window. This probably wont position the
picture in the center, just somewhere toward the center. In a later chapter, Ill show you
how to animate something so it winds up dead-center.
Things to notice:
To move the div right you use the keyword left. In the code above, the final
position is left 15%.
Again, the specification is inside curly brackets: {left: 15%}. As before: The
attribute name isnt in quotation marks. Its followed by a colon. The value is in
quotation marks.
You could specify the value in pixels.
The first line measures the screen width and stores it in the variable windowWidth.
The second line subtracts the width of the image from the width of the window, then
divides the remainder by two. This is the number of pixels to offset the image so its
centered horizontally. The number is concatenated with px, because jQuery needs it
in the animate() method. For example: .animate({left: 300px})
For more information on the parentheses used in the math above, see Chapter 7 of my
A Smarter Way to Learn JavaScript. For an explanation of concatenation, see Chapter
8 of that book. The combination of the number plus px is stored in the variable
imageHorizOffset. If the measured width is, say, 1000 pixels, the value of
imageHorizOffset is 300px.
The third line animates the picture, using the variable imageHorizOffset to
specify the number of pixels. Since its a variable, it isnt enclosed in quotation
marks.
If you wished, you could center an element by specifying the same offset from the
right, but Ill stick with specifying it from the left here, and in the exercises.
Heres a statement that flies an image to the right and down at the same time:
$(img#monarch).animate({marginLeft: +=200px, marginTop:
+=150px});
$(img#house_thumb).on(click, function() {
$(this).css({opacity: 0});
$(img#house_big).css({opacity: 1, position:
relative, top: -100px});
});
The position is fixed. An element that animates must have relative, absolute, or fixed
position.
The div is positioned 300 pixels to the leftleft: -300px. Since 300 pixels is
the width of the div, moving it left by that amount places it left of the screen, out of
sight.
The div is assigned a z-index of 1 so that when it moves onto the screen, itll cover
up the small, black, icon-only tab. For information on how z-index works, see
Chapter 85 of my
A Smarter Way to Learn HTML & CSS.
This is the styling for the heading in the expanded tabin this example, Mail:
div.full_tab h2 {
font-family: arial, helvetica, sans-serif;
font-size: 40px;
color: white;
float: right;
margin: 19px 20px 0 0;
}
Since the heading is within the div, itll be included in the animation.
You could make the code more succinct by combining both statements above:
$(button#read_more).on(click, function() {
$(body).scrollTop($(window).height());
});
You could specify the number of pixels to scroll, with a number:
$(button#read_more).on(click, function() {
$(body).scrollTop(250);
});
This is the code to select even rows and color their background-color grey:
$(tr:even).css(background-color, #ccc);
Note: Since jQuery begins counting at 0, the heading row at the top is row 0. The
row that begins with Bark is row 2. The row that begins with Puppy is row 4.
Normally, the heading row would be shaded, but Ive overridden the jQuery with CSS
styling for that row.
Alternatively, you could define a tr class with a grey background-color, and code it
this way:
$(tr:even).attr(class, shaded_row);
You can also select odd rows:
$(tr:odd).css(background-color, #ccc);
The code above produces this:
You can select even or odd rows in a class of tables or in a table with a particular id.
The following code selects even rows in a table whose id is pets.
$(table#pets tr:odd).css(background-color, #ccc);
You can use even and odd to select the even or odd items in any groupfor
example, the even or odd divs on the page, the even or odd paragraphs in a div, the even or
odd items in a list, etc.
When the customer clicks the first or second button, the next button over pitches a
sweeter deal for the user and a bigger sale for you. For example, if she clicks the middle
button, choosing 6 for $22, this happens:
The first two buttons have the class upsell. This is the code:
$(button.upsell).on(click, function() {
$(this).next().html($(this).next().text() + <br><span
class=urge_more>Save $2 more by choosing this deal!
</span>);
When the user clicks either of the first two buttons, the next element is selected.
Using the html() method, I concatenate its original textin this case 9 for $32
together with some red-colored text telling her she can save $2 more.
What happens if she initially clicks the third button, choosing the most expensive
option? As a commercially-minded person, I would be inclined to add a fourth button
offering her 12 for $42:
$(button#third).on(click, function() {
$(button#fourth).fadeTo(fast, 1);
});
In CSS, the fourth button is originally styled with 0 opacity, so its invisible. When
the third button is clicked, the fourth button fades up into visibility.
In the example, when one of the first two buttons is clicked, next() selects the
next button. But thats only because the next element in the document happens to be a
button. next() doesnt select the next element of the same kind. It selects the next
element of any kind. So, for example, if the user clicked a button that triggered a next()
selection and the next element were an image, the image would be selected.
Theres another way to target elementsby specifying their position in the DOM,
the Document Object Model.
The DOM is an organization chart, created automatically by the browser when your
webpage loads. All the elements in your HTML documentthe tags, the text blocks, the
images, the links, the tables, the style attributes, and morehave spots on this
organization chart. This means that your jQuery code can get its hands on anything on
your webpage, anything at all, just by saying where that thing is on the chart. Whats
more, your jQuery can add things, move things, or delete things by manipulating the chart.
If you wanted to (you wouldnt), you could almost create an entire webpage from scratch
using jQuerys DOM methods.
Heres a simplified webpage. Ive indented the different levels in the hierarchy. The
three top levels of the DOM are always the same for a standard webpage.
Under each of the top three levels are are more levels.
As you can see, every single thing on the webpage is included, even the title text and
the paragraph text. Lets make it a little more complicated by adding a div and a second
paragraph. Here it is in HTML form.
Document node
html node
head and body nodes
title node
div node
two paragraph nodes
three text nodes, one for the title and one for each of the two paragraphs.
In this particular chart, there are three types of nodes: document, element, and text.
The document node is the top level. Element nodes are <html>, <head>, <body>, <title>,
<div>, and <p>. Text nodes are the strings that comprise the title and the two paragraphs.
The <head> and <body> nodes are enclosed within the <html> node.
The <div> node is enclosed within the <body> node.
Two <p> nodes are enclosed within the <div> node.
A text node is enclosed within each of the <p> nodes.
When a node is enclosed within another node, we say that the enclosed node is a
child of the node that encloses it. So, for example, the <div> node is a child of the
<body> node. Conversely, the <body> node is the parent of the <div> node. Heres the
organization chart from the last chapter, showing all the parents and their children.
As you can see, <html> is the child of the document<head> and <body> are the
children of <html><div> is the child of <body>two <p>s are the children of
<div>each text node is the child of a <p>. Conversely, the document is the parent of
<html>, <html> is the parent of <head> and <body>, <head> is the parent of <title>,
<title> is the parent of a text node, and so on. Nodes with the same parent are known as
siblings. So, <head> and <body> are siblings because < html> is the parent of both. The
two <p>s are siblings because <div> is the parent of both.
Starting at the bottom of the chart, the text Nor to this. is a child of <p>, which is a
child of <div>, which is a child of <body>, which is a child of <html>, which is a child of
the document.
Now look at this markup.
<p>This is <em>important</em>!</p>
If you made a chart for this paragraph, you might think that all the text inside the
paragraph tags is a child of the <p> node. But remember, every node that is enclosed by
another node is the child of the node that encloses it. Since the text node important is
enclosed by the element node <em>, this particular text node is the child of <em>, not
<p>. The text nodes This is and ! as well as the element node <em> are siblings,
because theyre all enclosed by <p>. Theyre all children of <p>.
Find the interactive coding exercises for this chapter at:
http://www.ASmarterWayToLearn.com/jquery/63.html
64
Select the first child
or last child
Suppose your DOM structure looks like this:
<div>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Integer tincidunt.</p>
<p>Aenean commodo ligula eget dolor. Aenean massa.</p>
<p>Donec quam felis, ultricies nec, pellentesque eu,
pretium quis, sem.</p>
</div>
<div>
<p>Nulla consequat massa quis enim. Donec pede justo,
fringilla vel, aliquet nec, vulputate eget, arcu.</p>
<p>In enim justo, rhoncus ut, imperdiet a, venenatis
vitae, justo. Nullam dictum felis eu pede mollis pretium.
</p>
</div>
<div>
<p>Cum sociis natoque penatibus et magnis dis parturient
montes, nascetur ridiculus mus.</p>
<p>Cras dapibus. Vivamus elementum semper nisi. Aenean
vulputate eleifend tellus.</p>
<p>Aenean leo ligula, porttitor eu, consequat vitae,
eleifend ac, enim.</p>
<p>Aliquam lorem ante, dapibus in, viverra quis, feugiat
a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum.</p>
</div>
Thats three divs, each containing nothing but paragraphs. You want to increase the
font-size of the first paragraph in each div.
For the purposes of this example, Im going to assume that all paragraphs on the
page are the same font-size to begin with.
Since were assuming all paragraphs share the same font-size, we can measure any
of them to get the normal font-size. The following code arbitrarily selects the first
paragraph on the page as a sample paragraph, and measures its font-size:
var normal_size = $(p:first).css(font-size);
The original paragraph font-size is now stored in the variable normal_size. But
theres a complication. In my CSS file, I specified a font-size of 1.25em for paragraphs.
jQuery translates that into 20 pixels, which is fine. But the value stored in normal_size
isnt 20. Its 20px. I want to multiply it by 1.5 to enlarge the first paragraph in each div,
but the program balks if I try to multiply 20px by another number. The px part of it
means the program doesnt see it as a number and so cant multiply it. So I have to lop off
px, leaving just 20. Then the program will be able to do multiplication on it. To slice
off px, I use the slice() method, explained in Chapter 22 of my A Smarter Way to
Learn JavaScript.
var increased_size = normal_size.slice(0, normal_size.length
- 2) * 1.5;
The Javascript statement above slices off px, leaving just 20. Then it multiplies
by 1.5, getting 30. This number is stored in the variable increased_size. Thats
going to be the new size for every paragraph that is the first child in every div.
Now I code this:
$(p:first-child).css(font-size, increased_size + px);
$(p:first-child) selects every paragraph that is a first child of a parent.
So, for example, if you have three divs in the document that have the structure I showed
you at the beginning of this chapter, the code above selects the first paragraph in each of
those divs.
When the code above executes, the font-size of each targeted paragraph is increased
50%.
To select the last child instead of the first child, write $([element
type]:last-child). For example, $(img:last-child) selects all images
that are the last child of any parent.
Any type of element thats a child of something elsewhich is to say any type of
element except documentcan be selected this way. You cant select the document
this way, because it isnt the child of anything.
The selector begins with the element type, followed by a colon $(img:nth-
child(3))
Then you write$(img:nth-child(3))
Inside its own parentheses, you write the number: $(img:nth-child(3))
The code above targets the third image in an element only if the image is the third
child. This means, as you learned in the last chapter, that it doesnt necessarily target the
third image. For example, if an image is the third image but the fifth child because other,
non-image element types come before it in the element, it wont be selected. If you want
to target the third image even though it may not be the third child, you write
$(img:nth-of-type(3))
Once again, these types of selectors can be used with elements of any kind
paragraphs, headings, spans, divs, etc.
Its a colon:
$(p:not(.optional))
followed by not:
$(p:not(.optional))
and then an extra set of parentheses:
$(p:not(.optional))
This is the way of doing it that Ill test you on in the exercises.
There are two types of elements selected here, text boxes and textareas. As usual, the
selectors are inside a single set of quotation marks and are separated by a comma
followed by a space.
Text boxes are selected by $(input:text). Textareas are selected by a simple
$(textarea).
The event is focus. It means when the user enters the field with a click or Tab.
The code that turns the field back to white when the user clicks or Tabs out of it is
similar, with two differences:
$(input:text, textarea).on(blur, function() {
$(this).css(background-color, #fff);
});
1. The event is blur, meaning that the user has clicked or Tabbed out of the field.
2. The color is #fffwhite.
The variable e is in the parentheses that follow function. It refers to the event that
has triggered the function. You dont have to name it e. You can use any legal
variable name. But its conventional to use e, which stands for event, or use the
more explicit name, event.
e.type gives you the type of the eventthe event to which the variable e refers
mouseover, mouseout, click, dblclick, keydown, etc.
The selector has the usual two layers of wrapping: parentheses enclosing quotation
marks.
It begins with the type of element, li.
This is followed by a colon.
The colon is followed by contains.
The string were looking for is in parentheses. Note that the string itself is not
enclosed in quotation marks.