jQuery in Action
Managing the wrapped element set
37
in that the
end()
method (which we'll examine in section 2.3.6) can be used to
back out of the elements added via
add()
.
Bring up the Wrapped Set Lab page in your browser, enter the previous exam-
ple (exactly as shown), and click the Execute button. This should execute the
jQuery operation and result in the selection of all images with either an
ple (exactly as shown), and click the Execute button. This should execute the
jQuery operation and result in the selection of all images with either an
alt
or
title
attribute.
Inspecting the
HTML
source for the
DOM
Sample reveals that all the images
depicting flowers have
alt
attributes, the puppy images have
title
attributes,
and the coffee pot image has neither. Therefore, we should expect that all images
but the coffee pot would become part of the wrapped set. Figure 2.7 shows a
screen capture of the relevant page portions of the results.
but the coffee pot would become part of the wrapped set. Figure 2.7 shows a
screen capture of the relevant page portions of the results.
Command syntax: add
add(expression)
Adds elements, specified by the expression parameter, to the wrapped set. The expression
can be a selector, an HTML fragment, a DOM element, or an array of DOM elements.
can be a selector, an HTML fragment, a DOM element, or an array of DOM elements.
Parameters
expression
(String|Element|Array) Specifies what is to be added to the matched set.
This parameter can be a jQuery selector, in which case any matched
This parameter can be a jQuery selector, in which case any matched
elements are added to the set. If an HTML fragment, the appropriate
elements are created and added to the set. If a DOM element or an array
of DOM elements, they are added to the set.
of DOM elements, they are added to the set.
Returns
The wrapped set.
The wrapped set.
Figure 2.7 The expected image elements, those with an
alt
or
title
attribute, have been matched
by the jQuery expression.