Disclaimer: I have no experience using YUI at all.
I was wondering how the following lines of jQuery code would look when written in YUI. Also, since YUI is using a hierarchical dependency system, which .js files need to be included in order for the code to work?
1. Given the ID of an HTML element, apply multiple style rules on the element.
$('#foo').css({ color: 'yellow', background: 'black' });
2. Chaining: given an ID of an HTML element, apply a style rule on it, add a class of bar to it, and set its contents to ‘!’.
$('#foo').css('color', 'red').addClass('bar').html('!');
3. Append a LI element to #menu.
$('#menu').append('<li>An extra item</li>');
4. Basic event binding: show an alert whenever a LI element is clicked on.
$('li').click(function() {
alert('Clickety-click!');
});
Disclaimer: I’m not a YUI expert, there may be better ways to do these things.
Also, jQuery is good at doing what you’ve put up. YUI is built more for its widgets, so you may find its core functionality a little less comprehensive than jQuery’s (i.e., it doesn’t replace EVERYTHING you would ever want to do the DOM with function calls). I feel like one uses YUI 2.x because they want widgets (I use their menus quite a bit).
#1: You would need to include this:
Code would be:
Alternatively…
#2: There is no chaining in YUI 2.x, so your solution won’t be nearly the same:
I don’t care for chaining anyways, I think this code is much more readable. (Sorry for the editorializing.)
#3: Again, not sure how to set html directly besides just using innerHTML… I would think it’d just be:
#4: You’ll have to include different classes for this solution:
And here’s the code:
Again, sorry that these may not be the penultimate YUI solutions. Also, if you’re worried about the constant usage of “YAHOO.util.longname.method”, you can easily just make your own local variable. YUI does this all the time in their libraries: