var els;
// Select an element by id
var els = $("#container");
// Select elements by tag name
els = $("li");
// Select elements by class
els = $(".current");
/**
* Select elements by... selector!
* [can of worms]O~ ~ ~
*/
// Can select by parent/child relationship
els = $("div a"); // a anywhere underneath a div
els = $("div + div"); // div right after another div
els = $("div > p"); // p directly under a div
els = $("div ~ ul"); // any sibling ul to div
// Can select by their content
els = $("ul li:contains('sixth')");
// can select by their position
els = $("ul li:nth-child(3)");
els = $("ul li:last-child"); // every last
els = $("ul li:last"); // the very last
// even and odd position
els = $("ul li:odd");
// select elements by a combination of selectors
els = $("div.foo a");
// select elements by more than one class
els = $(".foo.bar");
// select elements that have one class or another
els = $(".foo, .bar");
// by attribute and attribute value
els = $("li[data-id]");
els = $("li[data-id='1']");
/**
* Refining selections.
*/
// div.foo elements that contain <p> tags
var els = $("div.foo").has("p");
// h1 elements that don't have a class of bar
var els = $("h1").add("div.foo.bar");
var els = $("h1").not(".bar ");
var els = $("h1:not(.bar)");
/**
* performing queries on a previous jQuery selection
*/
var $items = $("ul li");
// unordered list items with class of current
$items.filter(".current");
// just the first unordered list item
$items.first();
$items.eq(5);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.