23 March 2010 carlos.bueno.org/jq-yui.html
YUI3 includes features for dynamic loading and namespacing
Some parts of libraries are more popular than others. This first section is a mixed bag of popular idioms in YUI3 and jQuery.
jQuery uses the Sizzle CSS selector library, which is mostly CSS3-compliant but also has a great many extra pseudoclasses and extensions, some of which (:first
) are used very often while others (:header
) are obscure.
YUI3 can operate with three selector engines: browser native (the default), CSS2, and CSS3. This section documents mostly the differences between jQuery and the CSS3 specification.
The animation libraries of both libraries have substantial overlap, though jQuery makes it a bit easier to combine show() and hide() operations with animated effects.
The fundamental unit of jQuery is a Javascript Array containing 0 or more DOM elements. These Array objects have extra .on()
, .click()
, .map()
, etc methods attached to them in addition to the built-in list operations like .slice()
and .concat()
.
The fundamental units in YUI are Node objects, which wrap DOM elements, and NodeLists which are collections of Nodes. NodeLists are not Arrays and are not natively iterable.
jQuery 1.4.2 |
YUI 3.0.0 |
Getting Started |
$.foo.bar() |
YUI().use('node', 'module2', 'module3', function(Y) { Y.foo.bar() } ); |
The YUI3 is a little different. It is sandboxed and by default dynamically loaded. The |
jQuery 1.4.2 |
YUI 3.0.0 |
Common Idioms |
$('div.foo:first') |
Y.one('div.foo') |
jQuery and YUI3 use similar selector syntax, but jQuery has added extensions, mainly convenience pseudo-classes, to the Sizzle CSS3-compliant selector engine. YUI3 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) { // do something } |
var foo = Y.all('div.foo'); if (foo.size() > 0) { // 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') |
.setContent('foo') .set('text', 'foo') .set('value', 'foo') |
|
.html() .text() .val() |
.get('innerHTML') .get('text') .get('value') |
jQuery tends to overload getters and setters in the same method. |
.attr('foo') .attr('foo', 'bar') |
.get('foo') .set('foo', 'bar') |
Generic attribute getters and setters. |
.click(fn) .focus(fn) .blur(fn) .mouseout(fn) .mouseover(fn) // jQuery 1.4.2 and later allows you to // register events when creating the element $('<p/>',{text:'foo', click:fn}) |
.on('click', fn) .on('focus', fn) .on('blur', fn) .on('mouseout', fn) .on('mouseover', fn) |
|
parent.append('<div/>') |
parent.append('<div/>') |
Creates a new div element and makes it a child of |
parent = $('<div/>'); $('<p>foo<p>') .click(fn) .appendTo(parent); |
parent = Y.Node.create('<div/>'); child = Y.Node.create('<p>foo</p>'); child.on('click', fn); parent.appendChild(child); |
YUI3 builds element trees outside-in. jQuery can do both outside-in and inside-out (see next entry). YUI3 may add support for
|
child.appendTo(parent) |
parent.append(child) parent.appendChild(child) |
jQuery's YUI3's |
.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 a node's CSS class 'foo' with 'bar'.
|
.empty() |
.get('children').remove(true); |
jQuery's
|
.siblings() |
.get('parentNode').get('children') |
Note that the YUI3 code is not equivalent: it will contain all child elements including the caller. YUI3 may add support for
|
.show() .hide() |
.setStyle('display', null) .setStyle('display', 'none') |
YUI3 does not provide convenience wrappers for show/hide with animations and effects.
|
jQuery 1.4.2 |
YUI 3.0.0 |
Selectors |
$('*') |
Y.all('*') |
Select all nodes. Note that the default selector engine for YUI3 is CSS 2.1. For all examples in this section, use the |
$(':animated') |
Psuedoclass to select all elements currently being animated. No YUI3 equivalent. |
|
$(':button') |
Y.all('input[type=button], button') |
Extension. In both jQuery and YUI3 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. YUI3 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') |
Extension. 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 YUI3'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)'); |
Y.all(Y.all('div')._nodes.slice(n + 1)); Y.all(Y.all('div')._nodes.slice(0,n)); |
Extension. |
$('div:has(p)') |
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,h7') |
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. This is a weird one. In jQuery > 1.3.2 The YUI3 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 lst = Y.all('div'); if (lst) { var last = lst.item(lst.size()-1); } |
The YUI equivalent is cumbersome, but I'm not sure if |
$('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 |
jQuery 1.4.2 |
YUI 3.0.0 |
Effects |
$('#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(); |
The basic syntax and capabilities of both animation libraries are very similar. jQuery has convenience methods for effects like YUI3 has several easing algorithms built-in, and offers powerful tools such as animations over Besizer curves. Make sure to load the |
$('#.foo').fadeOut(); // or $('#.foo').hide(600); |
var a = new Y.Anim( { node: '#foo', to: {opacity: 0.0}, duration: 0.2, easing: Y.Easing.easeOut } ); a.on('end', function(ev) { ev.target._node .setStyle('display', 'none'); }); a.run(); |
Annoyingly, YUI Anim objects have events you can attach functions to, but you have to poke the private |
jQuery 1.4.2 |
YUI 3.0.0 |
Array vs NodeList |
$('.foo').array_method(args) |
Y.all(Y.all('.foo')._nodes.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 the private |
$('div').slice(x, y) |
Y.all(Y.all('div')._nodes.slice(x, y)) |
Return the xth to the yth div elements. |
$('div').concat($('p')) |
Y.all( Y.all('div')._nodes.concat( Y.all('p')._nodes ) ) |
|
$('.foo').each( function() { this.some_method(); } ); |
Y.all('.foo').each( function(node, idx, lst) { node.some_method(); } ); |
|
$('.foo').filter('.bar') |
Y.all('.foo').filter('.bar') |
The |
var fn = function(idx) { return this.property === 'value'; }; $('.foo').filter(fn); |
var filtered = []; Y.all('.foo').each( function(node) { if (node.get('property') === 'value') { filtered.push(node._node); } } ); filtered = Y.all(filtered); |
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) { some_function(el); } ); |
var mapped = []; Y.all('.foo').each( function(node) { mapped.push( some_function(node) ); } ); mapped = Y.all(mapped); |
jQuery's |
jQuery 1.4.2 |
YUI 3.0.0 |
Ajax |
$.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 'io' module. |
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'); |
var fn = function(txnid, o) { Y.one('#message').setContent( o.responseText ); } Y.io('/ajax/test.html', { on: { success: fn } }); |
Load the content of a given URL and replace the contents of |
jQuery 1.4.2 |
YUI 3.0.0 |
CSS |
.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() |
??? |
Computed height / width. Excludes padding and borders. |
.innerHeight() .innerWidth() |
??? |
Includes padding but not border |
.outerHeight() .outerWidth() |
.get('offsetHeight') .get('offsetWidth') |
Includes padding and border |
.position() // {left: 123, top: 456} |
.getXY() // [123, 456] |
Get the computed x,y coordinates. The coordinates are relative to the nearest ancestor element that is relatively or absolutely positioned. |
If you find a bug or want to add to this list, you can check out the project on GitHub or add a comment below. Enjoy!