Filter Selectors

The following table lists the filter selectors

TypeDescription
:animatedSelects elements that are animated
:eq()Selects elements that equal
:evenSelects even elements
:firstSelects the fi rst element
:gt()Selects elements greater than a value
:headerSelects all elements that are headers, like h1, h2, h3 and so on.
:lastSelects the last element
:lt()Selects elements less than
:not()Selects elements that don't equal a value
:oddSelects the odd elements

Filtering Forms

jQuery contains filters that make it easy to select form elements.

The following table summarizes all of the different form filters.

FilterDescription
:buttonSelects all button elements and elements of type button.
:checkboxSelects all elements of type checkbox.
:checkedSelects all elements that are checked.
:disabledSelects all elements that are disabled.
:enabledSelects all elements that are enabled.
:fileSelects all elements of type fi le.
:imageSelects all elements of type image.
:inputSelects all input, text area, select and button elements.
:passwordSelects all elements of type password.
:radioSelects all elements of type radio.
:resetSelects all elements of type reset.
:selectedSelects all elements that are selected.
:submitSelects all elements of type submit.
:textSelects all elements of type text.
 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var n = $(':input').length; 
                document.writeln(n); 
            });
        </script> 
    </head> 
    <body>
        <form name="" method="post"> 
            <input type="text" /> 
            <input type="hidden" /> 
            <input type="password" /> 
            <input type="radio" /> 
            <input type="reset" /> 
            <input type="submit" /> 
            <input type="image" /> 
            <input type="file" /> 
            <input type="checkbox" /> 
            <input type="reset" /> 
            <input type="button" value="hit me"/> 
            <button>Hit me too</button> 
            <select>
                <option>floor 1</option> 
                <option>floor 2</option> 
            </select>
            <textarea></textarea>
        </form> 
    </body> 
</html>
  
Click to view the demo

The following code shows a few different ways to get all of the elements in the same form.

 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>
            $(function(){ 
                var n1 = $("input").length; 
                var n2 = $(":input").length; 
                var n3 = $("form > *").length; 
                var n4 = $(":text").length; 
                var n5 = $("input[type='text']").length; 
                document.writeln(n1 + ","+ n2 +","+ n3 + "," + n4 + "," + n5); 
            });
        </script> 
    </head> 
    <body>
        <form name="" method="post"> 
            <input type="text" /> 
            <input type="text" /> 
            <input type="text" /> 
        </form> 
    </body> 
</html>
  
Click to view the demo

Filtering by Visibility

TypeDescription
:hiddenSelects all elements that are hidden.
:visibleSelects all elements that are visible.
 
<!DOCTYPE html> 
<html>
    <head>
        <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"> 
        </script>
        <script>

            $(function(){ 
                var numInv = $(":text:hidden").length; 
                var numVis = $(":text:visible").length; 
                document.writeln(numInv); 
                document.writeln(numVis);
            });

        </script> 
    </head> 
    <body>
        <form name="" method="post"> 
            <input type="text" name="text1" style="display:none;"/> 
            <input type="text" name="text2" style="offsetWidth:0; offsetHeight:0;"/> 
            <input type="text" name="text3" style="display:block;"/> 
        </form> 
    </body> 
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    jQuery  

Selector:
  1. jQuery Selector
  2. DOM Element Selection
  3. Filter Selectors
  4. Selecting by Position
  5. Filtering by Content
  6. Filtering by Relationships
  7. Escape characters
  8. ID attribute
  9. Tag Name
  10. Class Name
  11. Descendant
  12. Child (E > F)
  13. General sibling (E ~ F)
  14. Multiple expressions (E, F, G)
  15. Universal (*)
  16. Numbered child (:nth-child(n/even/odd/expr))
  17. First child (:first-child)
  18. Last child (:last-child)
  19. Only child (:only-child)
  20. Not (:not(E))
  21. Empty (:empty)
  22. Attribute selectors
  23. Attribute ([attr])
  24. Attribute equals ([foo=bar])
  25. Attribute does not equal ([foo!=bar])
  26. Attribute begins with ([foo^=bar])
  27. Attribute ends with ([foo$=bar])
  28. Attribute contains ([foo*=bar])
  29. Attribute contains word ([foo~=bar])
  30. Attribute contains prefix ([foo|=bar])
  31. Attribute exists $("[attributeName*='value']");
  32. Form selectors (:input)
  33. Form text fields (input:text)
  34. Form Password field (input:password)
  35. Form Radio button (input:radio)
  36. Form Checkbox (input:checkbox)
  37. Form Submit button (input:submit)
  38. Form Image button (input:image)
  39. Form Reset button (input:reset)
  40. Form button (input:button)
  41. Form File upload (input:file)
  42. Form Enabled form element (input:enabled)
  43. Form Disabled form element (input:disabled)
  44. Form Checked box (input:checked)
  45. Form Selected option (input:selected)
  46. Element at index (:eq(n))
  47. Greater than (:gt(n))
  48. Less than (:lt(n))
  49. First (:first)
  50. Last (:last)
  51. Even element (:even)
  52. Odd element (:odd)
  53. Is parent (:parent)
  54. Contains text (:contains(text))
  55. Contains element (:has(E))
  56. Visible (:visible)
  57. Hidden (:hidden)
  58. Header element (:header)
  59. Currently animating (:animated)
  60. $(this)
  61. Custom User Selectors