node-insert.mustache revision fb1ecdf65fd4106deb62da339763f2e7670ca0f6
2N/A<link href="{{componentAssets}}/node.css" rel="stylesheet" type="text/css">
2N/A<div class="intro">
2N/A <p>This example demonstrates how insert content when working with <code>NodeList</code> instances.</p>
2N/A <p>Clicking a box will update its content.</p>
2N/A</div>
2N/A
2N/A<div class="example">
2N/A{{>node-insert-source}}
2N/A</div>
2N/A
2N/A<h2>Setting up the NodeList</h2>
2N/A<p>First we need some HTML to work with.</p>
2N/A```
2N/A<ul id="demo">
2N/A <li>foo</li>
2N/A <li>bar</li>
2N/A</ul>
2N/A```
2N/A<h2>Adding Content</h2>
2N/A<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>
2N/A<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>
2N/A<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>
2N/A```
2N/A var onClick = function(e) {
2N/A var node = e.currentTarget;
2N/A node.prepend('<em>prepended</em> &nbsp;'); // added as firstChild
2N/A node.append('&nbsp; <em>appended</em>'); // added as lastChild
2N/A node.insert('&nbsp; <strong>before last child</strong> &nbsp;', node.get('lastChild')); // added before lastChild
2N/A node.insert('&nbsp; <strong>before childNodes[1]</strong> &nbsp;', 1); // added before childNodes[1]
2N/A };
2N/A```
2N/A
2N/A<h2>Attaching Events</h2>
2N/A<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>
2N/A```
2N/AY.all('#demo li').on('click', onClick);
2N/A```
2N/A
2N/A<h2>Complete Example Source</h2>
2N/A```
2N/A{{>node-insert-source}}
2N/A```
2N/A