modern-web-design-and-development

(Brent) #1

.is()


If, on the other hand, you want to target paragraphs that do have the class
someclass, you could be forgiven for thinking that this would do it:


1 $('p').is('.someclass').css('color', '#f90');

In fact, this would cause an error, because is() does not return elements:
it returns a boolean. It’s a testing function to see whether any of the chain
elements match the selector.


So when is is useful? Well, it’s useful for querying elements about their
properties. See the real-life example below.


:not()


:not() is the pseudo-selector equivalent of the method .not() It
performs the same job; the only difference, as with all pseudo-selectors, is
that you can use it in the middle of a selector string, and jQuery’s string
parser will pick it up and act on it. The following example is equivalent to
our .not() example above:


1 $('p:not(.someclass)').css('color', '#f90');

Real-Life Example


As we’ve seen, .is() is used to test, not filter, elements. Imagine we had
the following sign-up form. Required fields have the class required.


1 <form id='myform' method='post' action='somewhere.htm'>

(^2)

Free download pdf