You need to select an element based not only on its relationship to other elements or simple attribute values but also on varying characteristics such as programmatic states not expressible as selector expressions.
If you’re looking for an element with very specific characteristics, selector expressions may not be the best tool. Using jQuery’s DOM filtering method (filter()), you can select elements based on anything expressible with in a function.
The filter method in jQuery allows you to pass either a string (i.e., a selector expression)or a function. If you pass a function, then its return value will define whether certain elements are selected. The function you pass is run against every element in the current selection; every time the function returns false, the corresponding element is removed from the collection, and every time you return true, the corresponding element is not affected (i.e., it remains in the collection):
The preceding code selects all elements with a background image.
The initial collection is of all elements (*); then the filter() method is called with a function. This function will return true when a background Image is specified for the element in question. The !! that you see is a quick way of converting any type in Java-
In fact, the !! is not necessary because jQuery evaluates the return value into a Boolean it self, but keeping it there is still a good idea; anyone looking at your code can be absolutely sure of what you intended (it aids readability).
Within the function you pass to filter(), you can refer to the current element via the this keyword. To make it into a jQuery object (so you can access and perform jQuery methods), simply wrap it in the jQuery function:
Here are some other filtering examples to spark your imagination:
There will always be several different ways to do something; this is no less true when selecting elements with jQuery. The key differential is usually going to be speed; someway's are fast, others are slow. When you use a complicated selector, you should bethinking about how much processing jQuery has to do in the background. A longer and more complex selector will take longer to return results. jQuery’s native method scan sometimes be much faster than using a single selector, plus there’s the added benefit of readability. Compare these two techniques:
The second technique is shorter and much more readable than the first. Testing in Firefox (v3) and Safari (v4) reveals that it’s also faster than the first technique.
J Query Related Interview Questions
|HTML 4 Interview Questions||HTML Interview Questions|
|HTML 5 Interview Questions||Zend Interview Questions|
|JqueryUI Interview Questions||Dynamic HTML Interview Questions|
|XQuery Interview Questions||jQuery Mobile Interview Questions|
J Query Related Practice Tests
|HTML 4 Practice Tests||HTML Practice Tests|
|HTML 5 Practice Tests||Zend Practice Tests|
|PHP and Jquery Practice Tests||J Query Practice Tests|
|CodeIgniter Practice Tests||Dynamic HTML Practice Tests|
J Query Tutorial
Selecting Elements With Jquery
Beyond The Basics
Faster, Simpler, More Fun
Html Form Enhancements From Scratch
Html Form Enhancements With Plugins
Interface Components From Scratch
User Interfaces With Jquery Ui
Jquery Ui Theming
Jquery, Ajax, Data Formats: Html, Xml, Json, Jsonp
Using Jquery In Large Projects
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.