<b>Open the console to see the output of this code</b>
<h3>jQuery Selectors</h3>
<ul id="myList">
<li id="1st" class="listitem">First</li>
<li id="2nd" class="listitem">Second</li>
<li id="3rd" class="listitem">Third</li>
</ul>
<p class="listitem">This is the paragraph under the list</p>
<a href="http://www.extension.harvard.edu">This is a link</a>
JavaScript
// by Tag name
console.log("by tag name:");
console.log( jQuery('h3') );
console.log( jQuery('h3').html("This is my new h3 content") ); //with method call
// by ID
console.log("by id:");
console.log( jQuery('#1st') );
// by tag, id and DOM tree order
console.log("by tag, id, DOM tree order:");
console.log( $('ul#myList li') ); //multiple matches
// no match
console.log("no match, but still a return value:");
console.log( $('#thereIsNoId').html("hi") ); //no match, but a return value
// by other jQuery selectors
console.log("by other jQuery selectors:");
console.log( $('ul#myList .listitem:last') );
// Make a jquery object out of a plain DOMElement
// Use the element itself instead of a selector string
// as the argument to the jQuery function.
var el = document.getElementById("1st");
console.log("make a DOM element into a jQuery object");
console.log( $(el) );
// You can always get the DOM object from the jquery object
// be referecing its array(-like) elements
console.log( $('ul#myList .listitem:last')[0] );
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.