YUI 3.x Home -

YUI Library Examples: Node: DOM Methods

Node: DOM Methods

This example demonstrates how to use the DOM methods of a Node instance.

Click any of the boxes to move them to the other list.

  • lorem
  • ispum
  • dolor
  • sit
  • foo

Setting up the Node

First we need some HTML to work with.

  1. <ul id="demo">
  2. <li>lorem</li>
  3. <li>ispum</li>
  4. <li>dolor</li>
  5. <li>sit</li>
  6. </ul>
<ul id="demo">
    <li>lorem</li>
    <li>ispum</li>
    <li>dolor</li>
    <li>sit</li>
</ul>

Using DOM Methods

Most common DOM methods are available via Node instances. These can be used to add, remove, and retrieve other nodes.

Now we need a handler to move a node to a new list when the click event fires.

Note that appendChild returns a Node instance representing the node that was appended.

  1. var onClick = function(e) {
  2. var node = Y.one('#demo2').appendChild(e.currentTarget);
  3. node.addClass('yui-pass');
  4. };
var onClick = function(e) {
    var node = Y.one('#demo2').appendChild(e.currentTarget);
    node.addClass('yui-pass');
};

Listening for Node Events

We can assign our handler to all of the items by using the all method to get a NodeList instance and using the on method to subscribe to the event.

  1. Y.all('#demo li').on('click', onClick);
Y.all('#demo li').on('click', onClick);

Full Script Source

  1. YUI().use('node', function(Y) {
  2. var onClick = function(e) {
  3. var node = Y.one('#demo2').appendChild(e.currentTarget);
  4. node.addClass('yui-pass');
  5. };
  6.  
  7. Y.all('#demo li').on('click', onClick);
  8. });
YUI().use('node', function(Y) {
    var onClick = function(e) {
        var node = Y.one('#demo2').appendChild(e.currentTarget);
        node.addClass('yui-pass');
    };
 
    Y.all('#demo li').on('click', onClick);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings