cycling jQuery_Succinctly | Page 35

Using the is () method to return a Boolean value
It is often necessary to determine if the selected set of elements does , in fact , contain a specific element . Using the is () method , we can check the current set against an expression / filter . The check will return true if the set contains at least one element that is selected by the given expression / filter . If it does not contain the element , a false value is returned . Examine the following code :
Sample : sample23 . html
<! DOCTYPE html > < html lang =" en "> < body > < div id =" i0 "> jQuery </ div > < div id =" i1 "> jQuery </ div > < script src =" http :// ajax . googleapis . com / ajax / libs / jquery / 1.7.2 / jquery . min . js "></ script >
< script > ( function ($){ // Returns true . alert ($(' div '). is ('# i1 ')); // Returns false . Wrapper set contains no < div > with id =" i2 ". alert ($(' div '). is ('# i2 ')); // Returns false . Wrapper set contains no hidden < div > alert ($(' div '). is (': hidden '));
})( jQuery ); </ script > </ body > </ html >
It should be apparent that the second alert () will return a value of false because our wrapper set did not contain a < div > that had an id attribute value of i2 . The is () method is quite handy for determining if the wrapper set contains a specific element .
Notes : As of jQuery 1.3 , the is () method supports all expressions . Previously , complex expressions such as those containing hierarchy selectors ( such as +, ~, and >) always returned true . Filter is used by other internal jQuery functions . Therefore , all rules that apply there , apply here , as well . Some developers use is ('. class ') to determine if an element has a specific class . Don ' t forget that jQuery already has a method for doing this called hasClass (' class '), which can be used on elements that contain more than one class value . But truth be told , hasClass () is just a convenient wrapper for the is () method .
35