6a6a6d5448f8c49592581707a20e891abe577bfaDav Glass<link href="{{componentAssets}}/node.css" rel="stylesheet" type="text/css">
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<div class="intro">
e0b25dbc0322a078f384c45b48e5690f23d7eb15Dav Glass <p>This example demonstrates how insert content when working with <code>NodeList</code> instances.</p>
84e4fc4350d23a4c37b8d6a3f5ee4c131cacf5ddDav Glass <p>Clicking a box will update its content.</p>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass</div>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass<div class="example">
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass{{>node-insert-source}}
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass</div>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass<h2>Setting up the NodeList</h2>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass<p>First we need some HTML to work with.</p>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass```
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass<ul id="demo">
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass <li>foo</li>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass <li>bar</li>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass</ul>
55b12015bcdecf19f771f5387bc9b14af54da374Dav Glass```
e0b25dbc0322a078f384c45b48e5690f23d7eb15Dav Glass<h2>Adding Content</h2>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<p>Next we will add a handler to run when the event is fired. In our handler we will add content to the node.</p>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<p>Note that the event handler receives an event object with its <code>currentTarget</code> set to the current Node instance, and the actual node clicked as the <code>target</code>. The context of the handler is the NodeList instance, so <code>this</code> refers to our NodeList instance.</p>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<p>Note also that <code>prepend</code> inserts content as the <code>firstChild</code> of the Node instance, <code>append</code> inserts content as the <code>lastChild</code>, and <code>insert</code> places content before the specified node or childNode index.</p>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass```
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass var onClick = function(e) {
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass var node = e.currentTarget;
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass node.prepend('<em>prepended</em> &nbsp;'); // added as firstChild
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass node.append('&nbsp; <em>appended</em>'); // added as lastChild
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass node.insert('&nbsp; <strong>before last child</strong> &nbsp;', node.get('lastChild')); // added before lastChild
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass node.insert('&nbsp; <strong>before childNodes[1]</strong> &nbsp;', 1); // added before childNodes[1]
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass };
84e4fc4350d23a4c37b8d6a3f5ee4c131cacf5ddDav Glass```
84e4fc4350d23a4c37b8d6a3f5ee4c131cacf5ddDav Glass
762b21413a7bbc38b5c7b2d94385fb44f26f9d39Dav Glass<h2>Attaching Events</h2>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<p>We can assign our handler to all of the items by using the <code>all</code> method to get a <code>NodeList</code> instance and using the <code>on</code> method to subscribe to the event.</p>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass```
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav GlassY.all('#demo li').on('click', onClick);
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass```
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass<h2>Complete Example Source</h2>
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass```
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass{{>node-insert-source}}
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass```
fc2d50365b169f761de7a15d7a4fe406b33fd403Dav Glass