jQuery 1.8.3 | YUI 3.8.0 |
---|---|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script src="https://somedomain.com/path/to/plugin.js"></script> <script src="https://somedomain.com/path/to/anotherplugin.js"></script> <script> $(document).ready(function() { // Do stuff with the $ object here $.foo.bar() }); </script> Loading Method: Statically loaded (by default). Scope: The Plugins: To go beyond base library functionality, jQuery has the concept of "plugins" which are loaded in via additional
|
<script src="http://yui.yahooapis.com/3.8.0/build/yui/yui-min.js"></script> <script> YUI().use('module1', 'module2', 'module3', function (Y) { // Do stuff with the Y object here Y.foo.bar() }); </script> Loading Method: Dynamically loaded (by default). Scope: The Modules: Functionality in the YUI library is provided by "modules", which are split between core (officially supported) and gallery (user contributed). Simply adding YUI().use('node', 'io', 'event', 'animation', function (Y) { // example code here }); If any additional modules are needed, they'll be specified in the "notes" section to the example. |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$('div.foo:first') |
Y.one('div.foo') |
jQuery and YUI use similar selector syntax, but jQuery has added extensions, mainly convenience pseudo-classes, to the Sizzle CSS3-compliant selector engine. YUI comes with three different selector engines; see the section on Selectors. |
var foo = $('div.foo:first'); foo.some_method(); |
var foo = Y.one('div.foo'); if (foo) { foo.some_method(); } |
Return the first element which matches the selector. If no elements match,
|
$('div.foo') |
Y.all('div.foo') |
Select all div elements with a class of foo.
|
var foo = $('div.foo'); if (foo.length) { // do something } |
var foo = Y.all('div.foo'); if (foo.size()) { // do something } |
If no elements match the selector,
|
.find('p.foo:first') .find('p.foo') |
.one('p.foo') .all('p.foo') |
Finds |
$('<div/>') |
Y.Node.create('<div/>') |
Create a new DOM element. Does not add it to the document tree. |
.html('foo') .text('foo') .val('foo') |
.setHTML('foo') .set('text', 'foo') .set('value', 'foo') |
|
.html() .text() .val() |
.getHTML() .get('text') .get('value') |
jQuery tends to overload getters and setters in the same method. |
.attr('foo') .attr('foo', 'bar') |
.getAttribute('foo') .setAttribute('foo', 'bar') |
Generic HTML attribute getters and setters. |
$.trim(' foo ') |
Y.Lang.trim(' foo ') |
Strips leading and trailing whitespace. |
parent.append('<div/>') |
parent.append('<div/>') |
Creates a new div element and makes it a child of
|
child.appendTo(parent) |
child.appendTo(parent) |
Appends
|
parent = $('<div/>'); $('<p>foo<p>') .click(fn) .appendTo(parent); |
parent = Y.Node.create('<div/>'); Y.Node.create('<p>foo</p>') .appendTo(parent) .on('click', fn); |
Creates a new div element, then appends a p element with a click event subscription. Note that YUI's on() method is not chainable, so it returns an event handle, not the p node.
|
.replaceWith(content); |
.insert(content, 'replace'); |
Replace the specified node with content .
|
.before(content); |
.insert(content, 'before'); |
Insert content before the specified node.
|
.after(content); |
.insert(content, 'after'); |
Insert content after the specified node.
|
// Removes #foo from its parent and // detaches events and jQuery data. $('#foo').remove() // Removes #foo from #container. $('#container').remove('#foo') |
// Removes #foo from its parent, but doesn't // detach events or YUI plugins. Y.one('#foo').remove() // Removes #foo and detaches events and plugins. Y.one('#foo').remove(true) // Removes #foo from #container. Y.one('#container').removeChild(Y.one('#foo')) |
jQuery's |
.empty() |
.empty() |
Removes and destroys all of the nodes within the given node and also deregisters any events associated with the elements being destroyed.
|
.siblings() .siblings(selector) |
.siblings() .siblings(selector) .siblings(function) |
In addition to an optional selector string, YUI also supports passing a function to filter the returned siblings.
|
.next() .next(selector) |
.next() .next(selector) .next(fn) |
Same considerations as
|
.prev() .prev(selector) |
.previous() .previous(selector) .previous(fn) |
Same considerations as
|
.parent() |
.get('parentNode') |
Returns the parent node of the given node. |
.children() |
.get('children') |
Returns all the element children of the given node. |
.closest(selector) |
.ancestor(selector) .ancestor(fn) |
Returns the first ancestor that matches the given selector. In addition, YUI supports using a function instead of a selector to find an ancestor. |
$.contains(node, descendant) |
.contains(descendant) |
Check to see if a node contains a certain descendant. |
.show() .hide() |
.show() .hide() |
Make DOM nodes appear/disappear |
.fadeIn() .fadeOut() |
.show(true) .hide(true) |
In YUI,
|
$.parseJSON( '{"name":"Douglas"}' ) |
Y.JSON.parse( '{"name":"Douglas"}' ) |
Converts a JSON string into an object. Requires the |
Y.JSON.stringify({name: "Douglas"}) |
Converts an object to a JSON string. Requires the |
|
$.proxy(fn, context) |
Y.bind(fn, context) |
Creates a new function that will call the supplied function in a particular context. |
.data(key) .data(key, value) |
.getData(key) .setData(key, value) |
Stores data associated with a DOM element without modifying the DOM. |
.removeData() .removeData(key) |
.clearData() .clearData(key) |
Removes the associated data. |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$('#foo').click(fn) $('#foo').focus(fn) $('#foo').blur(fn) $('#foo').mouseout(fn).mouseover(fn) // jQuery 1.4.2 and later allows you to // register events when creating the element $('<p/>',{ text :'foo', className : 'bar', click : fn, focus : fn, blur : fn }) //Alternatively, you can use the bind() method $('#foo').bind('click', fn); |
Y.one('#foo').on('click', fn) Y.one('#foo').on('focus', fn) Y.one('#foo').on('blur', fn) Y.one('#foo').on('mouseout', fn) Y.one('#foo').on('mouseover', fn) // Alternatively, YUI allows you to attach multiple // subscribers with a single call. Y.one('#foo').on({ click : fn, focus : fn, blur : fn, mouseout : fn, mouseover: fn }) // Or attach a single subscriber to multiple events. Y.one("#foo").on(['click', 'focus', 'blur'], fn) |
In YUI, |
$('#container').delegate('#target', 'click', fn); |
Y.one('#container').delegate('click', fn, '#target'); |
Event listener will bind to #container and only fire the callback fn when a event's target is #target (or any other selector) |
$(document).on('click', '#target', fn); |
Y.one(Y.config.doc).delegate('click', fn, '#target'); |
jQuery's delegate() is generally preferred over bind() because of performance as well as chaining limitations (source).
|
$('#foo').trigger('click'); |
Y.one('#foo').simulate('click'); |
Simulates a click event. In YUI, you'll need the |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$('*') |
Y.all('*') |
Select all nodes. Note that the default selector engine for YUI is CSS 2.1. For all examples in this section, use the |
$(':animated') |
Psuedoclass to select all elements currently being animated. No YUI equivalent. |
|
$(':button') |
Y.all('input[type=button], button') |
Extension. In both jQuery and YUI you can run multiple selectors separated by commas. |
$(':checkbox') |
Y.all('input[type=checkbox]') |
Extension. |
$(':checked') |
Y.all(':checked') |
CSS3 |
$('parent > child') |
Y.all('parent > child') |
Immediate child selector (child must be one level below parent) |
$('parent child') |
Y.all('parent child') |
Descendent selector (child can be at any level below parent) |
$('div.class') |
Y.all('div.class') |
Class selector |
$(":contains('foo')") |
Y.all(':contains(foo)') |
Extension to select all elements whose text matches 'foo'. jQuery can take quotes or not. YUI requires no quotes. The text matching is plain string comparison, not glob or regexp. Be careful with this one as it will return all matching ancestors, eg |
$(':disabled') $(':enabled') |
Y.all(':disabled') Y.all(':enabled') |
CSS3. |
$(':empty') |
Y.all(':empty') |
CSS3. Selects all elements that have no child nodes (excluding text nodes). |
$(':parent') |
Y.all(':not(:empty)') |
Inverse of |
$('div:eq(n)') |
Y.all('div').item(n) |
Extension. Selects nth element. YUI's |
$('div:even') $('div:odd') |
Y.all('div').even() Y.all('div').odd() |
Extension. Selects all even or odd elements. Note that elements are 0-indexed and the 0th element is considered even. See also YUI's |
$(':file') |
Y.all('input[type=file]') |
Extension. Find input elements whose type=file. |
$('div:first-child') |
Y.all('div:first-child') |
CSS3. Selects the first child element of divs. |
$('div:first) |
Y.one('div') |
The |
$('div:gt(n)'); $('div:lt(n)'); // Or $('div').slice(n + 1); $('div').slice(0,n); |
Y.all('div').slice(n + 1); Y.all('div').slice(0, n); |
Extension. |
$('div:has(p)') |
var nodes = []; Y.all('div').each(function (node) { if (node.one('p')) { nodes.push(node); } }); nodes = Y.all(nodes); |
Extension. Selects elements which contain at least one element that matches the specified selector. In this example, all |
$(':header') |
Y.all('h1,h2,h3,h4,h5,h6') |
Extension. Selects all heading elements. Rarely used. |
$('div:hidden') |
var hidden = []; Y.all('div').each(function(node) { if ((node.get('offsetWidth') === 0 && node.get('offsetHeight') === 0) || node.get('display') === 'none') { hidden.push(node); } }); hidden = Y.all(hidden); |
Extension. In jQuery > 1.3.2 The YUI equivalent would essentially be a port of the jQuery code that implements |
$('#id') |
Y.all('#id') |
CSS3. Identity selector. |
$('input:image') |
Y.all('input[type=image]') |
Extension. Selects all inputs of type image. |
$(':input') |
Y.all('input,textarea,select,button') |
Extension. Selects all user-editable form elements. |
$(':last-child') |
Y.all(':last-child') |
CSS3. |
$('div:last') |
var list = Y.all('div'), len = list.size(), last; if (len) { last = list.item(len - 1); } |
Extension. Selects the last element matched by the selector. |
$('input[type=checkbox][checked]') |
Y.all('input[type=checkbox][checked]') |
CSS3, multiple attribute selector |
$(':not(div)') |
Y.all(':not(div)') |
CSS3. Negation selector. |
$(':password') |
Y.all('input[type=password]') |
Extension. |
$(':radio') |
Y.all('input[type=radio]') |
Extension. |
$(':reset') |
Y.all('input[type=reset]') |
Extension. |
$(':selected') |
Y.all('option[selected]') |
Extension. |
$(':submit') |
Y.all('input[type=submit]') |
Extension. |
$(':text') |
Y.all('input[type=text]') |
Extension. Does not select |
.is(selector) |
.test(selector) |
Checks elements against a selector and returns a boolean indicating whether or not it matches. |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$('#foo').animate( { width: 100, height: 100, opacity: 0.5 }, { duration: 600, easing: 'swing' } ); |
var a = new Y.Anim( { node: '#foo', to: { width: 100, height: 100, opacity: 0.5 }, duration: 0.6, easing: Y.Easing.bounceOut } ); a.run(); // Or use transition Y.one('#foo').transition( { width: 100, height: 100, opacity: 0.5 duration: 0.6, easing: 'ease-out' } ); |
The basic syntax and capabilities of both animation libraries are very similar. jQuery has convenience methods for effects like YUI has several easing algorithms built-in, and offers additional tools such as animations over Bezier curves. Requires the You can also use YUI's |
$('#.foo').fadeOut(); // Or $('#.foo').hide(600); |
Y.one('#foo').hide(true) |
jQuery's jQuery effects tend to default to 200 or 600ms while YUI's show/hide transitions default to 500ms. YUI durations are in fractions of seconds; jQuery durations are set in milliseconds. |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$('.foo').array_method(args) |
Y.all('.foo').array_method(args) |
Any Array operation that you can perform on a jQuery list can be translated to YUI in this form. YUI NodeList objects are not native Arrays, but do provide wrapper functions for the most common array methods as of 3.3.0. |
$('div').slice(x, y) |
Y.all('div').slice(x, y) |
Return the xth to the yth div elements. |
$('div').add('p') |
Y.all('div').concat(Y.all('p')) |
Add nodes that match the specified selector. |
$('.foo').each( function() { $(this).some_method(); } ); |
Y.all('.foo').each( function() { this.some_method(); } ); |
|
$('.foo').filter('.bar') |
Y.all('.foo').filter('.bar') |
The |
$('.foo').filter(function (idx) { return this.property === 'value'; }); |
Y.all('.foo').filter(function (node) { return node.get('property') === 'value'; }); |
Classic functional programming filter function. Given a list of elements, run the function on each and return a list of those which evaluated true. |
$('.foo').map( function(idx, el) { return some_function(el); } ) |
var mapped = []; Y.all('.foo').each( function (node) { mapped.push(some_function(node)); } ); mapped = Y.all(mapped); |
jQuery's |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
$.ajax({ url: url, data: data, success: successFn }); |
Y.io(url, { data: data, on: {success: successFn} }); |
YUI.io has extra options for failure mode callbacks, headers, cross-frame i/o, etc. jQuery.ajax() has some interesting options for async, context, and filtering. Make sure to load the YUI |
Y.io(url, { data: data, on: {success: successFn}, xdr: {use: 'flash'} }); |
Cross-domain requests via a Flash helper. No jQuery equivalent. |
|
$('#message').load('/ajax/test.html'); |
Y.one('#message').load('/ajax/test.html'); Y.one('#message').load('/ajax/test.html', '#foo'); |
Load the content of a given URL and replace the contents of In YUI, the |
$.getJSON(url, successFn); |
Y.io(url, { on: {success: successFn} }); |
A remote request for a JSON file. Same-origin policy is enforced. There is no convenience method for this in YUI. |
$.getJSON( "/some/jsonpURL.json?callback=?", successFn ); |
Y.jsonp( "/some/jsonpURL.json?callback={callback}", successFn ); |
JSONP requests. Same-origin policy is not enforced. Requires the |
jQuery 1.8.3 | YUI 3.8.0 | Notes |
---|---|---|
.addClass('foo') .removeClass('foo') .toggleClass('foo') .hasClass('foo') |
.addClass('foo') .removeClass('foo') .toggleClass('foo') .hasClass('foo') |
CSS class name manipulation. |
.removeClass('foo').addClass('bar') |
.replaceClass('foo', 'bar') |
Replace node's CSS class 'foo' with 'bar'. |
.css('display', 'block') |
.setStyle('display', 'block') |
Set a single CSS property |
.css({ height: 100, width: 100, display: 'block' }) |
.setStyles({ height: 100, width: 100, display: 'block' }) |
Set multiple CSS properties with a dictionary. |
.css('display') |
.getStyle('display') |
Get the current value for a CSS property. |
.height() .width() |
.getComputedStyle('height') .getComputedStyle('width') |
Computed height / width. Excludes padding and borders. |
.innerHeight() .innerWidth() |
.get('clientHeight') .get('clientWidth') |
Includes padding but not border |
.outerHeight() .outerWidth() |
.get('offsetHeight') .get('offsetWidth') |
Includes padding and border |
.offset() // {left: 123, top: 456, width: 789, height: 1011} |
.getXY() // [123, 456] |
Get the computed x,y coordinates relative to the document. jQuery also returns the size of the node |
.offset({ left: 123, top: 456 }) |
.setXY(123, 456) |
Set the x,y coordinates relative to the document. |
jQuery 1.4.3 | YUI 3.8.0 | Notes |
---|---|---|
$.each([1, 2, 3], fn(index, value)) $.each({ key: value }, fn(key, value)) |
Y.Array.each([1, 2, 3], fn(value, index)) Y.Object.each({ key: value }, fn(value, key)) |
Iterate through an array or object. YUI is compatible with the Array forEach method so the first parameter the callback receives when iterating through an array is the value. In jQuery, the first parameter is the index of the value in the array. |
$.inArray(value, array) |
Y.Array.indexOf(array, value) |
Returns the index of the searched value in the specified array. |
$.type(obj) |
Y.Lang.type(obj) |
Returns a string representing the type of the specified object. YUI and jQuery results are compatible (see jQuery's). |
$.isPlainObject(obj) |
Y.Lang.isObject(obj) Y.Lang.isObject(obj, true) |
In YUI, |
$.isArray(obj) $.isFunction(obj) |
Y.Lang.isArray(obj) Y.Lang.isFunction(obj) Y.Lang.isString(obj) Y.Lang.isBoolean(obj) Y.Lang.isDate(obj) Y.Lang.isNumber(obj) Y.Lang.isNull(obj) Y.Lang.isUndefined(obj) Y.Lang.isValue(obj) |
YUI also has some extra type checking functions. In particular, |
$.isEmptyObject(obj) |
Y.Object.isEmpty(obj) |
Check if the given object doesn't have any properties. |
$.makeArray(obj) |
Y.Array(obj) |
Make an array-like object, for instance the return value of |
$.now() |
Y.Lang.now() |
Return the current time in milliseconds. |
The jQuery - YUI 3 Rosetta Stone was originally created by Carlos Bueno. It's now maintained by Ryan Grove and Paul Irish.
Please file bugs and recommend changes on GitHub. You're also more than welcome to fork the GitHub repo and send pull requests.