JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB...

47
JAVASCRIPT AND JQUERY : AN INTRODUCTION (WEB PROGRAMMING, X452.1) Professional Program: Data Administration and Management Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 9

Transcript of JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB...

Page 1: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

JAVASCRIPT AND JQUERY: AN INTRODUCTION

(WEB PROGRAMMING, X452.1)

Professional Program: Data Administration and Management

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Class 9

Page 2: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

AGENDA

22. Advanced jQuery

22.1 Handling Events with jQuery

22.2 jQuery Animations

22.3 jQuery Selectors

22.4 jQuery Selection Methods

22.5 jQuery Utility Functions

Page 3: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

Advanced jQuery

22.

Page 4: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

jQuery defines a uniform event API that works in all browsers

jQuery is easier to use, yet more powerful than regular JS,

especially when it comes to event handling

Simple event-registration methods for each of the commonly

used and universally implemented browser events

This method uses internally the on() (formerly bind()) method to

bind the event handler

Using the on() method allows for more flexibility and advanced

features

Furthermore, it also

enables you to register

custom events and to

deregister events

335

Page 5: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Simple Event Handler Registration

Calling a jQuery event-registration method registers your handler

on all of the selected elements, easier than doing it one by one

Simple event handler registration methods jQuery defines:

Focus and blur events

do not bubble

focusin and focusout

events do bubble

mouseover and mouseout

events do bubble

mouseenter and mouseleave do not bubble

jQuery ensures that these events work in all browsers

336

Page 6: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Two special events in addition to simple event handlers:

hover():

Registers handlers for mouseenter and mouseleave events

Calling hover(f,g) is like calling mouseenter(f) and then calling mouseleave(g)

toggle():

Binds event handler functions to the click event

Allows you to specify two or more event handler functions subsequently invoked for

each click event

Pass any HTML string into $(), second arg can be an object

Additionally, use event property to register an event handler

function

337

Page 7: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

jQuery Event Handlers

Simple event handler expects no arguments and returns no value

But internally jQuery uses arguments and pays attention to the returning value

First argument is always a jQuery Event object

Return value = false default action and propagation is stopped (same as preventDefault() and stopPropagation())

jQuery Event Object

jQuery hides implementation differences between browsers by defining its own Event object

jQuery copies all of the fieldsfrom the native Event object into every jQuery Event object:

338

Page 8: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

jQuery event object

defines the

following methods:

Some jQuery Event object fields are worth mentioning:

339

Page 9: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Additional

fields:

340

Page 10: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Advanced Event Handler Registration

Using on() (formerly bind()) directly allows you to use advanced event registration features that are not available through the simpler methods

Simplest on() form expects an event type string as its first argument and an event handler function as its second

Advanced form uses three arguments:

Event type, data, event handler

Multiple event handlers:

Use namespaces for event handler registration to uniquely identify event handlers:

To bind an event handler in a namespace:

Add a period and the namespace name to the event type string

341

Page 11: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Final feature of on() is that the first argument can be an object that

maps event names to handler

functions:

one() method:

Invoked and works just like on()

Event handler you register will automatically deregister itself after it is invoked

Deregistering Event Handlers

Deregister event handler with off() (formerly unbind()) to prevent it

from being triggered by future events

With no arguments, off() deregisters all event handlers (all event

types) for all elements

in the jQuery object:

One string argument

for all event types for all

selected elements:

342

Page 12: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

If you used namespaces,

then deregister only

those events:

To deregister a specific event handler, use the reference of the

element and the two argument

version of off():

Use off() with single

argument object:

One more way that off() can be invoked:

Passing a jQuery Event object to it unbinds the event handler that that

event was passed to

Calling off(ev) is equivalent to off(ev.type, ev.handler)

343

Page 13: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

344

Page 14: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Triggering Events

Manually trigger the event by

using the simple event handler method:

Events that bubble will bubble even when triggered manually like

this

jQuery’s event triggering methods will trigger any handlers

registered with jQuery’s event registration methods, and they will

also trigger handlers defined on HTML attributes or Element

properties such as onsubmit

But they will not trigger any events registered with

addEventListener() !!

Specialized form of triggering events:

Or use general trigger method:

345

Page 15: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Can only specify one event type in method trigger!

Use namespaces for event triggers to trigger

only events in that namespace:

Handlers registered through properties like onclick are

considered to have no namespace

You can also pass an Event

object or a plain object:

Type property determines

what event to trigger

Allows to pass additional

data as properties

(i.e., synthetic)

346

Page 16: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.1 HANDLING EVENTS WITH JQUERY

Pass additional data to event handlers when you trigger them

manually by using the

second argument to trigger():

Trigger all handlers for a given event type, use $(‘*’) to select all

elements, then call trigger very inefficient

Instead, use utility function to process this more efficiently:

jQuery.event.trigger()

Takes the same arguments as trigger()

After triggering event handlers, default actions are performed

To trigger without default action, use triggerHandler() Calls

the preventDefault() and cancelBubble() methods of the Event

object

347

Page 17: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Simple methods such as fadeIn() and fadeOut() for basic visual effects

Use animate() method instead for more complex custom animations

Every animation has a duration specifying how long effect should last

Specify this as a number of milliseconds or by using a string:

The string “fast” means 200ms

The string “slow” means 600ms

If jQuery does not recognize duration string default duration of 400ms

Define new names by adding string-to-number mappings to jQuery.fx.speeds

jQuery’s effect methods usually take effect duration as an optional

first argument:

If you omit the duration argument,

you usually get the default 400ms

jQuery’s effects are asynchronous, 2nd argument is a callback function

Callback function is invoked once for each selected element

348

Page 18: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Passing a callback function to an effect method allows you to

perform actions at the end of an effect

By default, jQuery’s

animations are queued

Calling an animation on an already animated element is delayed

until previous animation is complete

jQuery’s effect methods

are declared to accept

optional duration and

callback arguments or

use an object

specifying animation

properties

349

Page 19: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Simple Animation

jQuery defines nine simple effects methods to hide and show

elements

Divided into three groups based on kind of effect they perform

Fading

Main methods: fadeIn(), fadeOut(), fadeTo()

fadeIn() and fadeOut() simply animate the CSS opacity property to show or

hide an element

fadeTo() method: Duration (or options object) is required as first argument

and target opacity is required as the second argument

Visibility

Main methods: show(), hide(), toggle()

fadeOut() method retains space of element, hide() removes element

toggle() changes the visibility state of the elements it is invoked on

350

Page 20: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Sliding

Main methods: slideDown(), slideUp(), slideToggle()

slideUp() hides the elements in the jQuery object by animating their height

to 0 and then setting the CSS display property to “none”

slideDown() reverses the process to make a hidden element visible again

slideToggle() toggles the visibility of an item using a slide up or slide down

animation

Each of the three methods accepts the optional duration and callback

arguments (or the options object argument)

351

Page 21: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Disabling Animations

jQuery makes it easy to globally disable all effects: simply set

jQuery.fx.off to true

Effect of changing the duration of every animation to 0ms, making them

behave as instantaneous, nonanimated changes

To allow end users to disable effects, you might use code like

this in your scripts

352

Page 22: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Easing Functions

The straightforward but naive way to perform animations

involves a linear mapping between time and the value being

animated

Visual effects are more pleasing if they are not linear

jQuery calls the easing function with a time-based value

between 0 and 1

Returns another value between 0 and 1 and jQuery computes

the value of the CSS property based on this computed value

jQuery’s default easing function is a sinusoid: it starts off slow,

then speeds up, then slows down again to “ease” the animation

to its final value

Adding custom easing function:

353

Page 23: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Custom Animation

Use the animate() method to produce more general animated effects than are available with the simple effects methods

1st argument: animation properties object specifies what to animate and the remaining arguments specify how to animate it

Simulate slideUp() method:

Optional arguments:

2nd arg: duration, 3rd arg: easing function, 4th arg: callback

Or pass an options object as second argument:

Or in the most general case, animate() accepts two object arguments:

Animation Properties Object

Animation Options Object

354

Page 24: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Animation Properties Object(1st Argument)

Property names for this object must be CSS attribute names,

and the values of those properties(must be numeric) must be

the target values toward which the animation will move

To specify relative values, prefix the value string with “+=” to

increase the value or with

“-=” to decrease the value

In addition to numeric values there are three other values:

hide, show, toggle

Replace the property values with

“show” or “toggle” to produce sideways

slide effects analogous to slideDown()

and slideToggle()

355

Page 25: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Animation Options Object (2nd Argument)

Is an optional object that holds options that specify how the

animation is performed

Most common options: duration and complete

queue property of the options object specifies whether the

animation should be queued (deferred until any pending

animations have completed, call

to animate() is queued by default!)

Width animation begins at the same

time fadeIn() effect begins, but fadeOut() effect begins as soon

as the fadeIn() effect ends

Animation options object:

Animation parameters:

356

Page 26: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Finally, jQuery’s animationframework even allows you tospecify different easing functions for the different CSS properties you want to animate

Canceling, Delaying, Queuing Effects

stop() method stops any currently executing animations on the selected elements

Two optional Booleanarguments:

When animations are triggered by user events, you may want to cancel any current or queuedanimations before beginninga new one

357

Page 27: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

Method delay(): Simply adds a timed delay to the animation

queue: pass a duration in milliseconds (or a duration string) as

the first argument and a queue name as the optional second

argument:

Final animation-related

methods gives you

low-level access to the

jQuery queuing mechanism:

jQuery queues are lists of functions to be executed in sequence

Add a new function to the queue with the

queue() method, function is invoked in order of queue:

When your function reaches

the head of the queue, it will

be automatically dequeued and invoked

358

Page 28: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.2 JQUERY ANIMATION

To manipulate

the queue:

Also, jQuery defines versions of the queue() and dequeue()

methods as utility

functions

jQuery’s effects and animation methods use a queue named

“fx”, and this is the queue that is used if you do not specify a

queue name

Queue mechanism is useful whenever you need to perform

asynchronous operations sequentially:

Instead of passing a callback function to each asynchronous operation so

that it can trigger the next function in the sequence, you can use a queue

to manage the sequence instead

359

Page 29: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

jQuery supports a fairly complete subset of the selector grammar defined by the CSS3 Selectors draft standard, with the addition of some nonstandard but very useful pseudo classes

Simple Selectors

Simple selector begins with a tag type specification

Use tag name (“p”), or wildcard (“*”), or nothing (=wildcard).

Tag name or wildcard form the base selector followed by filters

Filters are applied left to right

Example:

Selects paragraphs that are the first or every third subsequent child of their parent, as long as they contain the word “JavaScript” and do not contain an <a>element

Filters typically run most efficiently if prefixed with a tag type:

Rather than simply using “:radio” to select radio buttons, for example, it is better to use “input:radio”

360

Page 30: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

361

Page 31: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

362

Page 32: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

Selector Combination

Simple selectors can

be combined using

special operators or

“combinators” to

represent

relationships between elements in the document tree

363

Page 33: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

364

Page 34: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.3 JQUERY SELECTORS

Selector Groups

A selector group, which is the kind of selector that we pass to

$()(or use in a stylesheet), is simply a comma-separated list of

one or more simple selectors or selector combinations

Note that the CSS and jQuery selector syntax uses parentheses

for some of the filters in simple selectors, but it does not allow

parentheses to be used more generally for grouping

365

Page 35: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Selection methods alter the set of selected elements by refining,

augmenting, or just using it as starting point for a new selection

Modifying Current Selection

Position within Selection

Simplest way to refine a selection is by

position within the selection:

366

Page 36: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Refining Selection

The general method for refining a selection by position is slice()

Accepts a start and an end index (with negative indexes

measured from the end of the array) and returns a jQuery object

that contains elements from the start index up to, but not

including, the end index

Filter Method

filter() is a

general-purpose

selection filtering

method

Invoke it in 3

different ways:

367

Page 37: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Not() Method

not() method is just like filter(), except that it inverts the sense of the filter

Has() Method

has() method is another way to refine a selection

Returns a new jQuery object that contains only the selected elements that have a descendant that matches the selector

Add() Method

add() method augments a selection rather than filtering or refining it

add() returns the originally selected elements plus whatever elements would be selected (or created) by the arguments if thosearguments were passed to $()

368

Page 38: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Selection as Context

filter(), add(), and not() methods perform set intersection, union,

and subtraction operations on independent selections

jQuery defines a number of other selection methods that use the

current selection as the context

Find() Method

Searches descendants of each of the currently selected elements for

elements that match the specified selector string

Then returns a new jQuery object that represents that new set of

matching descendants

Children() Method

Returns the immediate child elements of each selected element,

filtering them with

an optional selector

369

Page 39: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Contents() Method

contents() method is similar to children(), but it returns all child nodes, including text nodes, of each element

Next(), Prev() Methods

next(), prev() methods return thenext/previous sibling of each selected element that has one

nextAll(), prevAll() return all siblings following and all siblings preceding (if there are any)each selected element

nextUntil() and prevUntil() methods take a selector argument and select all siblings following or preceding the selected element until a sibling is found that matches the selector

If you omit the selector, these methods work just like nextAll() and prevAll() with no selector

370

Page 40: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Parent(), Parents() Methods

parent() method returns the parent of each selected element

parents() method returns ancestors of each selected element

parentsUntil() returns the ancestors of each selected element

until the first ancestor that matches the specified selector

closest() method requires a selector string and returns the

closest ancestor (if any) of each selected element that matches

the selector

371

Page 41: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

Reverting to a Previous Selection

To facilitate method chaining, most jQuery object methods return the object on which they are called

However, selection methods return a new jQuery object

End() Method

end() in a method chain restores the set of matched elements to its previous state

To manually define the set of selected elements in a way that is compatible with the end() method, pass the new set of elements as an array or array-like object to the pushStack() method

The elements you specify become the new selected elements, and the previous set of selected elements is pushed on the stack, where they can be restored with end()

372

Page 42: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.4 JQUERY SELECTION METHODS

andSelf(), addBack() Method

andSelf() returns a new jQuery object that includes all of the

elements of the current selection plus all of the elements (minus

duplicates) of the previous selection

andSelf() works like the add() method, but is now deprecated

addBack() is the replacement method

373

Page 43: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.5 JQUERY UTILITY FUNCTIONS

The jQuery library defines a number of utility functions (as well as two properties) that you may find useful in your programs

jQuery.browser

The browser property is not a function but an object that you can use for client sniffing

Client sniffing is best avoided whenever possible, but you can use this property to work around browser-specific bugs with code like this:

jQuery.contains()

This function expects two document elements as its arguments

Returns true if first element contains second element and returns false otherwise

jQuery.each()

Unlike the each() method which iterates only over jQuery objects, the jQuery.each() utility function iterates through the elements of an array or the properties of an object

374

Page 44: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.5 JQUERY UTILITY FUNCTIONS

jQuery.extend()

This function expects objects as its arguments

Copies properties of second and subsequent objects into first object,

overwriting any properties with the same name in the first argument

jQuery.globalEval()

This function executes a string of JavaScript code in the global context, as

if it were the contents of a <script>element

jQuery.grep()

It expects an array as its first argument and a predicate function as its

second, and it invokes the predicate once for each element in the array,

passing the element value and the element index

375

Page 45: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.5 JQUERY UTILITY FUNCTIONS

jQuery.inArray()

1st argument is search value, 2nd argument is an array, returns first index in array at which match is found

jQuery.isArray()

Returns true if the argument is a native Array object

jQuery.isEmptyObject()

Returns true if the argument has no enumerable properties

jQuery.isFunction()

Returns true if the argument is a native Function object

jQuery.isPlainObject()

Returns true if the argument is a “plain” object rather than an instance of some more specialized type or class of objects

jQuery.makeArray()

If the argument is an array-like object, this function copies the elements of that object into a new (true) array and returns that array

376

Page 46: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.5 JQUERY UTILITY FUNCTIONS

jQuery.map()

It expects an array or array-like object as its first argument and a function

as its second

Passes each array element along with the index of that element to the

function and returns a new array that collects the values returned by the

function

jQuery.merge()

This function expects two arrays or array-like objects. It appends the

elements of the second to the first and returns the first

377

Page 47: JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB …ucb-access.org/Files/JS/PPPresentation/PPJSClass9.pdf · Two special events in addition to simple event ... is equivalent to off(ev.type,

22.5 JQUERY UTILITY FUNCTIONS

jQuery.proxy()

It takes a function as its first argument and an object as its second and

returns a new function that invokes the function as a method of the

object

jQuery.support

This is a property like jQuery.browser, but it is intended for portable

feature testing rather than more brittle browser testing

jQuery.trim()

This function is like the trim() method added to strings in ES5

Expects a string as its only argument and returns a copy of that string with

leading and trailing whitespace removed

378