Chapter 7: jQuery Flashcards

1
Q

What is JQuery?

A

It’s a javascript file that you include in your web pages. It let’s you find elements using cs style selectors and then do something with the selection using methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when you make a selection

A

It’s stored in a jQuery object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do I make a jQuery seelction

A

$(‘css-slector’)

e.g. $(‘li.hot’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you use a jQuery method

A

Like an object methods, by do notation

e.g. $(‘li.hot’).addClass(‘complete’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why use jQuery?

A
  • Simple selectors
  • common tasks in less code
  • cross browser compatibility
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are other ways of describing a jQuery object?

A

matched set

jQuery selection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you access a specific item in a jQuery selection

A

Array notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Some jQuery methods get and set element contents. What needs to be kept in mind?

A

If a jquery object has multiple elements, and we a method, say .html(). only the content from the first element is returned

If we use the html() method to update contents of a jQuery selection it changes all elements in the jQuery selection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What can we do to make variables containing jQuery selections more easily recognised?

A

giver it a $sign at start of name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is implicit iteration?

A

jQuery’s ability to automatically update all matching nodes in a jQuery object (No need to loop through the results)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if we have an object that has a long list of chained methods and one of those methods is incorrectly spelt?

A

None will run.

If one does not run, none will.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we check if DOM is ready to work with?

A

load event = once DOM and all resources and media loaded
.ready() = as soon as DOM loaded

$( document).ready(function(){
//Your script
});
or the shorthand
$(function(){
//Your script
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do the .html() and .text() methods do?

A

Retrieve and update content of elements

Caution with .html() - XSS

.html() - retrieves only the html inside the first match

.text() - retirves text from all matching elements plus their decedents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why do we need to be careful using .replaceWith and .html() methods?

A

Same risks as innerHTML - XSS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are four methods to update content of a jQuery Object?

A

.html() - updates all elements html
.text() - updates all element text
.replaceWith() - adds new content and returns replaced content
.remove() - removes the matching elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would use and amend content in a jQuery selection

A

Pass a function as a parameter

$(‘li.hot’).html(function(){
return ‘VemV>’ + $(this).text() + ‘V/emV’;
});

This would wrap the text of the selected elements in an em tag

$(this) here refers to the currently selected element

17
Q

What methods control inserting elements

A

Before() and after() - These will put insertion before/after a tag

prepend() and append() - These put the insertion inside the selected tag

18
Q

How can you add or remove:

an attribute
a class

A

attr() / removeAttr()

addClass() /removeClass()

19
Q

How do you get and set css properties

A

.css()

if you want to set multiple properties pass it css rules as a js object literal

$(‘li’).css({
‘background-color’: ‘grey’,
‘font-family’: ‘Georgia’
});

20
Q

What does the each method do?

A

It allows you to loop through a selection and make changes to it. Things you can’t do through implicit iteration.

This includes getting information from each element in the set
Perform a series of actions on elements

The each() method takes a function sa parameter

21
Q

What’s the difference between this and $(this)?

A

this - when using an each method, this accesses the current element that’s elected

$(this) - creates a jquaery object with the current selection. This allows you to use jQuery methods on it.

22
Q

When do we use the .on() method?

A

This is for events

The on method takes two parameters: an event and a function

$(‘li’).on(‘click’, function(){
$(this).addClass(‘complete’);
});

23
Q

What additional parameters can the on() method take

A

Filter the initial jQuery selection to respond to s upset of elements
Pass extra information into the event handler using object literal notation

the [ ] only indicate the parameter is optional

.on(events [, selector][, data], function(event object));

24
Q

What effects does jQuery provide

A

show()/hide()/toggle()

fadeIn()/fadeOut()/fadeTo()/fadeToggle()

slideUp()/slideDown()/slideToggle()

delay()/stop()/animate()

25
Q

What does the animate method do

A

It allows us to animate css property changes

You can animate a propperty whose value is a number

Write them in camecase e.g borderRadius

Properties are specified in object literal notation

26
Q

What are the three optional paramters for the animate method

A

speed, easing, complete

27
Q

What is a callback function?

A

a function that runs once a process is complete

28
Q

What do the find() and closest() methods have in common?

A

They both require a css selector.

find() -finds all elements within current selection that match selector

closest() - nearest ancestor (not just parent) that matches the selector

29
Q

What methods exist for traversing the dom in jQuery?

A

.parent()
.parents() - All parents of current selection
.children()
.siblings()
.next() - next sibling of current element
.nextAll() - All subsequent siblings of current element
.prev()
.prevAll() - All previous siblings of current element

30
Q

How can we add elements to a selection?

A

The add() method

31
Q

What methods exist to filter a selection

A

filter() - filters selection by a seccond selector
find()
.not() / :not()
.has() / :has()
:contains() - selects elements that contain specified text

32
Q

What does the .is() do?

A

It checks if current selection matches a condition.

It returns a boolean.

33
Q

How can we access items returned ina jQuery object?

A

Using array syntax

Some useful methods:
.eq() - element that matches the index number
:lt() - elements with an index number less than what’s specified
:gt() - elements with an index number greater than what’s specified