Universal (*)
Description and Syntax
$('*')
selects all elements.
Examples
$('*')
all elements in the document$('p > *')
all elements that are children of a paragraph element
<!DOCTYPE html>
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js">
</script>
<script>
<!-- w w w . j a v a2s.c o m-->
$(document).ready(function(){
var allElements = $("*");
document.writeln(allElements.length);
});
</script>
<body>
<div id="main">
<ul id="myList">
<li>A</li>
<li>B</li>
</ul>
</div>
</body>
</html>
The code above generates the following result.
Select all elements under a certain element
The following code selects all elements under a certain id.
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!-- ww w . j av a 2 s . c om-->
$("#main > *").css("border", "3px solid red");
});
</script>
</head>
<body>
<body>
<span id="main">
<div>asdf</div>
<button>Child</button>
<span>A Span</span>
</span>
</body>
</html>
The code above generates the following result.
Match all elements
The following code selects every element (including head
, body
, etc).
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--from w w w. j ava 2 s .com-->
$("*").css("border","3px solid red");
});
</script>
</head>
<body>
<body>
<div>A div</div>
<button>0</button>
<button>1</button>
<button>2</button>
<button>3</button>
<span></span>
</body>
</html>
The code above generates the following result.
...
Selector syntax
ID
Tag Name
Class Name
Descendant $('E F')
Child (E > F)
General sibling (E ~ F)
Multiple expressions (E, F, G)
Numbered child (:nth-child(n/even/odd/expr)...
First child (:first-child)
Last child (:last-child)
Only child (:only-child)
Not (:not(E))
Empty (:empty)
Attribute selectors
Attribute existence([attr])
Attribute equals ([foo=bar])
Attribute not equal ([foo!=bar])
Attribute begins with ([foo^=bar])
Attribute ends with ([foo$=bar])
Attribute contains ([foo*=bar])
Attribute contains word ([foo~=bar])
Attribute contains prefix ([foo|=bar])
Attribute exists $("[attributeName*='value'...
Form selector
Form input selector (:input)
Form text fields (input:text)
Form Password field (input:password)
Form Radio button (input:radio)
Form Checkbox (input:checkbox)
Form Submit button (input:submit)
Form Image button (input:image)
Form Reset button (input:reset)
Form button (input:button)
Form File upload (input:file)
Form Enabled form element (input:enabled)
...
Selector syntax
ID
Tag Name
Class Name
Descendant $('E F')
Child (E > F)
General sibling (E ~ F)
Multiple expressions (E, F, G)
Universal (*)
Filtering by RelationshipsNumbered child (:nth-child(n/even/odd/expr)...
First child (:first-child)
Last child (:last-child)
Only child (:only-child)
Not (:not(E))
Empty (:empty)
Attribute selectors
Attribute existence([attr])
Attribute equals ([foo=bar])
Attribute not equal ([foo!=bar])
Attribute begins with ([foo^=bar])
Attribute ends with ([foo$=bar])
Attribute contains ([foo*=bar])
Attribute contains word ([foo~=bar])
Attribute contains prefix ([foo|=bar])
Attribute exists $("[attributeName*='value'...
Form selector
Form input selector (:input)
Form text fields (input:text)
Form Password field (input:password)
Form Radio button (input:radio)
Form Checkbox (input:checkbox)
Form Submit button (input:submit)
Form Image button (input:image)
Form Reset button (input:reset)
Form button (input:button)
Form File upload (input:file)
Form Enabled form element (input:enabled)
...