5
<title>Example: DOM Methods</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: DOM Methods</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css">
23
<p>Click any of the blue boxes to copy to the other stack.</p>
24
<p>Click a box in the other stack to remove it completely.</p>
40
<script type="text/javascript">
41
YUI().use('node', function(Y) {
42
var onClick = function(e) {
43
var item = e.currentTarget,
44
list2 = Y.one('#demo2');
46
if (item.get('parentNode') === list2) { // remove if list2
47
item.remove(); // sugar for item.get('parentNode').removeChild(item);
49
if (list2.all('li').size() < 1) { // hide the list if its empty
53
if (list2.getStyle('display') === 'none') {
57
list2.append(item.cloneNode(true));
62
Y.one('#demo').delegate('click', onClick, 'li');
63
Y.one('#demo2').delegate('click', onClick, 'li');
69
<h2>Setting up the HTML</h2>
70
<p>First we need some HTML to work with.</p>
71
<pre class="code prettyprint"><ul id="demo">
72
<li>lorem</li>
73
<li>ipsum</li>
74
<li>dolor</li>
75
<li>sit</li>
78
<ul id="demo2">
79
<li>foo</li>
80
<li>bar</li>
81
</ul></pre>
83
<h2>Using DOM Methods</h2>
84
<p>Most common DOM methods are available via Node instances. These can be used to add, remove, and retrieve other nodes.</p>
85
<pre class="code prettyprint">clone = item.cloneNode(true);
87
item.remove(); // sugar for item.get('parentNode').removeChild(item);</pre>
90
<h2>Complete Example Source</h2>
91
<pre class="code prettyprint"><ul id="demo">
92
<li>lorem</li>
93
<li>ipsum</li>
94
<li>dolor</li>
95
<li>sit</li>
98
<ul id="demo2">
99
<li>foo</li>
100
<li>bar</li>
103
<script type="text/javascript">
104
YUI().use('node', function(Y) {
105
var onClick = function(e) {
106
var item = e.currentTarget,
107
list2 = Y.one('#demo2');
109
if (item.get('parentNode') === list2) { // remove if list2
110
item.remove(); // sugar for item.get('parentNode').removeChild(item);
112
if (list2.all('li').size() < 1) { // hide the list if its empty
116
if (list2.getStyle('display') === 'none') {
120
list2.append(item.cloneNode(true));
125
Y.one('#demo').delegate('click', onClick, 'li');
126
Y.one('#demo2').delegate('click', onClick, 'li');
128
</script></pre>
133
<div id="sidebar" class="yui3-u">
137
<div class="sidebox">
139
<h2 class="no-toc">Examples</h2>
143
<ul class="examples">
146
<li data-description="Using selectors and property accessors with Node.">
147
<a href="properties.html">Set and Get Properties</a>
152
<li data-description="Using DOM methods with Node.">
153
<a href="dom-node.html">DOM Methods</a>
158
<li data-description="Listening for DOM events with Node instances.">
159
<a href="events.html">Handling DOM Events</a>
164
<li data-description="NodeList provides Node functionality for multiple nodes.">
165
<a href="nodelist.html">NodeList</a>
170
<li data-description="Using a single event listener to handle events on multiple nodes.">
171
<a href="node-evt-delegation.html">Delegating Node Events</a>
176
<li data-description="This example demonstrates how to position an element in page coordinates.">
177
<a href="node-xy.html">Node Positioning</a>
182
<li data-description="This example demonstrates how to set styles and get style information.">
183
<a href="node-style.html">Node Styling</a>
188
<li data-description="This example demonstrates how to insert content into a Node.">
189
<a href="node-insert.html">Adding Node Content</a>
194
<li data-description="This example demonstrates how to show and hide a Node.">
195
<a href="node-view.html">Showing and Hiding</a>
219
<div class="sidebox">
221
<h2 class="no-toc">Examples That Use This Component</h2>
225
<ul class="examples">
246
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
247
<a href="../event/basic-example.html">Simple DOM Events</a>
252
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States.">
253
<a href="../node-focusmanager/node-focusmanager-1.html">Accessible Toolbar</a>
258
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
259
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
264
<li data-description="Use IO to request XML data from a remote web service.">
265
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a>
270
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources.">
271
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a>
276
<li data-description="Example Photo Browser application.">
277
<a href="../dd/photo-browser.html">Photo Browser</a>
282
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
283
<a href="../dd/portal-drag.html">Portal Style Example</a>
295
<script src="../assets/vendor/prettify/prettify-min.js"></script>
296
<script>prettyPrint();</script>