jQuery & CSS3 Selectors

HTML5 & CSS3 for the Real World Mobile HTML5: Using the Latest Today by Estelle Weyl and Maximiliano Firtman

Table of Contents

Basic CSS Selectors

<ul>
<li id="myID" class="myClass">blah</li>
<li>splat</li>
</ul>
li tag name $('li')
#myID ID $('#myID')
.myClass class $('.myClass')

Relational Selectors CSS testjQuery test

ul li
descendant selector
matches nested <li>s
ol > li
child selector
matches <li>s in <ol> but not nested <ul>
no need to use $('ul').children('li')
li.myClass + li
adjacent sibling
matches #4 only
NEW
li.myClass ~ li

general sibling selector
matches #4 and later, but not nested.

Ancestor Selector (jQuery Only) CSS FailjQuery test

<li>This has a <span>Span</span><p>
<li>This does not</li>
li:has(span)
E:has(F) Matches all ancestoral elements with tag name E that have at least one descendant matching F.

jQuery selector. Not a CSS Selector. There are more jQuery selectors below.

ABC's of jQuery SelectorsCSS testjQuery test

$('CSS Selector(s)')
$('li.myClass').text('blah');
$('li:first-of-type').text('blah'); 

Native JavaScript

<ul>
<li id="myID" class="myClass">blah</li>
<li>splat</li>
</ul>
li$('li')getElementsByTagName('li') 
#myID$('#myID')getElementById('myID') 
.myClass$('.myClass')getElementsByClassName('myClass')  IE9

Native JavaScript

<ul>
<li id="myID" class="myClass">blah</li>
<li>splat</li>
</ul>
li$('li') querySelectorAll('li')
#myID$('#myID')querySelector('#myID')
.myClass$('.myClass')querySelectorAll('.myClass')

Do not include jQuery
just for $(selector)
when native JavaScript will work

$('li:first-of-type')[0].text('blah'); 

same as

document.querySelector('li:first-of-type').innerHTML = 'blah'; 

CSS Selectors

Selector Categories

Attribute Selectors

element[attribute]
Select elements containing the named attribute
   img[alt] {} 
selects: <img src="image.jpg" alt="accessible"> does not select: <img src="image.jpg" title="inaccessible">
form [type] {}
selects: <input type=date> does not select:
<select>

IE6 sucks
IE7 has some support
jQuery makes it all work!

Attribute Selectors CSS testjQuery test

E[attr]
Element E that has the attribute attr with any value.
E[attr="val"]
Element E that has the attribute attr with the exact, case-insensitive, value val.
E[attr|=val]
Element E whose attribute attr has a value val or begins with val- ("val" plus "-").
p[lang|="en"]{/*    <p lang="en-us">  <p lang="en-uk"> */ }
E[attr~=val]
Element E whose attribrute attr has within its value the space-separated full word val.
a[title~=more] {/* <a title="want more info about this?">}

Attribute Selectors (not IE6/IE7) CSS testjQuery test

E[attr^=val]
Element E whose attribute attr starts with the value val.
a[href^=http]:after {content: " (" attr(href) ")";}
a[href^=mailto] {background-image: url(emailicon.gif);}
  
E[attr$=val]
Element E whose attribute attr ends in val .
a[href$=pdf] {background-image: url(pdficon.gif);}
a[href$=pdf]:after {content: " (PDF)";}
E[attr*=val]
Element E whose attribute attr matches val anywhere within the attribute. Similar to E[attr~=val] above, except the val can be part of a word.

Note: Multiple attributes work! a[title][href]

CSS Specificity

!mportant
1-0-0-0-0
inline
1-0-0-0
ID
1-0-0
Class
attribute selector
pseudo-class
0-1-0
Element
pseudo-element
0-0-1

Structural SelectorsCSS testjQuery test

First, Last & Only CSS testjQuery test

<ul>
  <li>bleep</li>	
  <li><em>blah</em></li>
  <li>splat</li>
  <li>bloak</li>
</ul>

First, Last & Only

:first-child
Works as is in CSS
:last-child
Works as is in CSS
:first-of-type
:first is slightly different
Same as :eq(0)
:last-of-type
:last is last of all, not per parent
:only-child
Works as is in CSS
:only-of-type
Maybe use :first:last instead

-of-types support NOT added by jQuery

nth-of-type CSS testjQuery test

<ul>
  <li>bleep</li>	
  <li>blah</li>
  <li>splat</li>
  <li>bloak</li>
  <li>grolt</li>
</ul>

nth-of-type

li:nth-child(4)
Works as is in both jQuery and CSS
li:nth-last-child(3)
li:nth-of-type(2n+3)
Use li:gt(n) instead
li:nth-last-of-type(4n)

Exclusions CSS testjQuery test

a:not([href^=http])
li:not(:last-of-type)
li:not(:nth-of-type(3))
li:not(.myClass)

jQuery Only Structural Selectors

E:first
The first element on the page matching E.
equal to E:first-of-type, E:nth-of-type(1), and jQuery's E:eq(0)
E:last
The last element on the page matching E.
equal to E:last-of-type
E:even
Every other E starting with #2
equal to E:nth-of-type(even)
E:odd
Every other E starting with #1
equal to E:nth-of-type(odd)
E:eq(Y)
n-th of elements matching E. Starts at 0
Similar to :nth-of-type(Y)
but counting starts at 0
E:gt(Y)
Every element E appearearing after E:(Y)
similar to E:nth-of-type(n+Y) or E:nth-of-type(Y)~E
but counting starts at 0, or
E:eq(Y)~E
E:lt(Y)
Every element E appearearing Before E:eq(Y)
similar to E:not(E:nth-of-type(n+Y))
counting starts at 0

:first-child, :last-child, :only-child, :nth-child supported in both jQuery and CSS

More jQuery Selectors

E:has(F)
E that has at least one descendant matching F
No CSS equivalent
E:contains(string)
E containing case sensitive string as child
No CSS equivalent
E:parent
E that is parent of another element, including text nodes.
use E:not(:empty) in CSS
E:empty
Exists in CSS. Opposite of :parent
:header
Same as h1, h2, h3, h4, h5, h6 {} in CSS

Selectivzr

www.selectivizr.com

<script src="jquery.js"></script>
<!--[if lte IE8>
<script src="selectivizr.js"></script>
<![endif]-->

SuperSelectors

http://skulljackpot.com/2009/04/19/super-css-selector-support-with-jquery/

UI Selectors (CSS &JQ)

:checked
radio buttons and checkboxes that are checked.
:disabled
disabled with attribute or via javascript
:enabled
Selects E that are NOT disabled
$('input:not(:disabled)')
button:enabled {}
:selected
Selects all elements that are selected.

Note: <input type="hidden"> can neither be enabled or disabled.

UI Selectors (jQ only)

Selects element based on state and/or attribute, but does not change the state

E:animated
E currently being animated with jQuery (jQ)
E:hidden
selects all hidden elements (jQ)
E:visible
selects all visible elements (jQ)

:hidden v. :visible

:hidden includes:

:visible includes all visible elements AND:

During animations that hide an element, the element :visible until the end of the animation.
During animations to show an element, the element is :visible during the entire animation.
Items currently animating their visibility (such as with .hide(), .show() or .slideUp() ) are :visible until the animation end.

Definitely add specificity for :hidden, since $('*:hidden') will grab <option>, <script>, <title>, etc.

<input> pseudoclasses (jQ only)

:button
<button> + <input type="button">
:checkbox
<input type="checkbox"> $('[type=checkbox]')
:file
<input type="file"/> $('[type=file]')
:image
<input type="image"/>$('[type=image]')
:password
<input type="password"/>$('[type=password]')
:radio
<input type="radio"/>$('[type=radio]')
:reset
<input type="reset"/>$('[type=reset]')
:submit
<input type="submit"/>$('[type=submit]')*
:text
<input type="text"/>$('[type=text]')
:hidden
<input type="hidden"/> $('[type=hidden]')
// QUIRKY: so specify input:hidden

<input> tidbits

pseudo-class selectors ( begin with ":") imply the universal selector ("*") . Recommend preceding with selector so "*" is not implied.

remember submit event is applied to <form> not to <button> or <input type=submit>

:input <input> + other form controls, $('input, textarea, button, select')
does not include meter, progress or output

New <input> types?

color
date
datetime
datetime-local
email
month
number
range
search
tel
time
url
week

The 13 new input types are NOT supported with :color, etc, but [type=color] work fine.

New HTML form element attributes

form
autocomplete
autofocus
list
pattern
required
placeholder
multiple
list
min
max
step

CSS Pseudo classes & elements

Work in CSS, Not in jQuery

E:link
E:visited
No longer fully supported in all browsers for security
E:active
E:hover
E:focus
E:target
E within or as the target of the URI
E::first-line
E's first line
E::first-letter
E's first letter
E::selection
portion of E element currently selected/highlighted by user
E::before
generated content before E
E::after
generated content after E

CSS Form PseudoClasses

:default
:valid
:invalid
:in-range
:out-of-range
:required
$(''form [required]')
:optional
$(''form :not([required])')
:read-only
$(''form [readonly]')
:read-write
$('form :not([readonly])')

Thank You

Newticorn