Web Development with jQuery®

(Elliott) #1

(^44) ❘ CHAPTER 2 SELECTING AND FILTERING
if (node.is('[required]'))
{
In the context of this example, you just want to know whether the element is required, and that
is done by using is() to ask whether the element has the required attribute. Because the required
attribute is a boolean attribute and its only possible value is required, or to not exist at all, then the
simplest way of asking if the element is required is by using the attribute selector [required].
is() can be used to ask any question of an element or collection of elements that can be expressed as
a selector. When it comes to multiple elements being present in a selection, the question that you ask
using is() is true if it matches any of the elements in the selection. If the selector matches only a sin-
gle element, but not the other elements, the result is still true. The result is only false if it matches
none of the elements present in the selection.
If the element has the required attribute, the expression node.is('[required']) will return true, and
the program will then examine the value of the input. The value of the input is retrieved with a call
to val(), another jQuery method that does some behind-the-scenes work to make it a lot easier to
fetch the value of a fi eld, automatically adjusting its logic based on the type of input fi eld that you
are working with. It returns the value of the value attribute in the case of an element (no
matter the type); it retrieves the selected