UNPKG

ullamlaboriosam

Version:
939 lines (907 loc) 79.4 kB
<!DOCTYPE html><html><head><title>Documentation - Umbrella JS</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="keywords" content="javascript, js, library, umbrella, html, html5, light"><meta name="description" content=" Lightweight and intuitive javascript library"><meta property="og:url" content="http://umbrellajs.com/"><meta property="og:title" content="Umbrella JS"><meta property="og:image" content="http://umbrellajs.com/umbrella.png?1"><meta property="og:description" content="Lightweight and intuitive javascript library to speed up your web development"><link href="fontello.css" rel="stylesheet"><link href="picnic.css" rel="stylesheet"><link href="style.css" rel="stylesheet"><link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet" type="text/css"><link rel="icon" href="umbrella.svg" type="image/svg+xml"></head><body><nav><div class="brand"><img src="umbrella.svg" alt="Logo" class="logo"><a href="/">Umbrella JS</a></div><div class="menu"><a href="https://github.com/franciscop/umbrella" class="pseudo button icon-g"><span class="text">Github</span></a><a href="documentation" class="button icon-doc"><span class="text">Documentation</span></a></div></nav><main id="home" class="documentation"><section class="flex five"><aside class="full fifth-1000"><h2>Contents</h2><div class="links flex two three-500 five-800 one-1000"></div><a href="#home" tabindex="-1" class="top pseudo button">▲ Up you go ▲</a></aside><article class="full four-fifth-1000"><h1 id="documentation">Documentation</h1> <p>Find nodes from the HTML with a CSS selector:</p> <pre><code class="lang-js">u(&#39;ul#demo li&#39;) u(document.getElementById(&#39;demo&#39;)) u(document.getElementsByClassName(&#39;demo&#39;)) u([ document.getElementById(&#39;demo&#39;) ]) u( u(&#39;ul li&#39;) ) u(&#39;&lt;a&gt;&#39;) u(&#39;li&#39;, context) </code></pre> <h3 id="parameters">Parameters</h3> <p>The first parameter can be:</p> <ul> <li>A text CSS selector</li> <li>A single HTML Node. This is specially useful in events where you can just pass <code>this</code></li> <li>A NodeList or other similar objects that can be converted to an array</li> <li>An array of nodes*</li> <li>Another Umbrella instance</li> <li>An HTML fragment as a string</li> <li>Nothing</li> </ul> <p>The second parameter is only for the CSS selector, which indicates a portion of the DOM where the selector is applied. For example, with <code>u(&#39;li&#39;, u(&#39;ul&#39;).first())</code> it will find all of the <code>li</code> from the first <code>ul</code>.</p> <p>* actually it can be an array of anything you want as in <code>[&quot;a&quot;, &quot;b&quot;]</code>, however this is not officially supported and might change at any moment</p> <blockquote> <p>You <em>should</em> use <code>u(&#39;#demo&#39;)</code> instead of <code>u(document.getElementById(&#39;demo&#39;))</code>, internally it&#39;s optimized to do this in a fast way. That was only an example of what&#39;s possible.</p> </blockquote> <h3 id="return">Return</h3> <p>An instance of Umbrella JS so you can chain it to any of the other methods.</p> <h3 id="examples">Examples</h3> <p>Select all of the list elements that are children of <code>ul</code></p> <pre><code class="lang-js">var lis = u(&#39;ul &gt; li&#39;); // Same as u(&#39;ul&#39;).children(&#39;li&#39;); </code></pre> <p>Find all of the headers from the page to create a Table of Contents:</p> <pre><code class="lang-js">var headers = u(&#39;h1, h2, h3, h4, h5, h6&#39;); </code></pre> <p>Generate a link on the fly:</p> <pre><code class="lang-js">var link = u(&#39;&lt;a&gt;&#39;).addClass(&#39;main&#39;).attr({ href: &#39;/hello&#39; }); </code></pre> <p>You can use this to generate many kind of elements on the fly. For example, for a simple grocery list (using ES6 for simplicity):</p> <pre><code class="lang-js">var fruits = [&#39;apple&#39;, &#39;strawberry&#39;, &#39;pear&#39;, &#39;banana&#39;]; var list = u(&#39;&lt;ul&gt;&#39;).append(fruit =&gt; `&lt;li&gt;${ fruit }&lt;/li&gt;`, fruits); u(&#39;body&#39;).append(list); </code></pre> <p>It plays well with other libraries, including jquery. For example, with <a href="http://github.com/franciscop/pagex">pagex.js</a>:</p> <pre><code class="lang-js">// When we are on the page &quot;/login&quot; page(/^login/, function(){ function done(err, res){ if (err) return alert(&quot;There was an error&quot;); window.location.href = &quot;/user/&quot; + res.id; }; // Find the form and handle it through ajax when it&#39;s submitted u(&quot;form.login&quot;).ajax(done); }); </code></pre> <h3 id="native-methods">Native methods</h3> <blockquote> <p>This section is inspired by <a href="http://blissfuljs.com/docs.html#vanilla">Bliss.js&#39; vanilla methods</a></p> </blockquote> <p>There are many native methods and properties that you can use. These can be called straight in the <code>.first()</code> or <code>.last()</code> elements, a <code>.nodes</code> element or you can loop every element to call them. For example:</p> <pre><code class="lang-js">// Single element from .nodes u(&#39;h1&#39;).nodes[0].classList.add(&#39;vanilla&#39;); // Single element u(&#39;h1&#39;).first().classList.add(&#39;vanilla&#39;, &#39;test&#39;); // Multiple elements. Note that the order is different from jquery u(&#39;h2&#39;).each(function(el){ el.classList.add(&#39;vanilla&#39;, &#39;test&#39;); }); </code></pre> <p>And for the arrays it&#39;s similar, you can call any array method on <code>u().nodes</code> since this is literally an array:</p> <pre><code class="lang-js">u(&#39;h2&#39;).nodes.forEach(); var mapped = u(&#39;h2&#39;).nodes.map(); var filtered = u(&#39;h2&#39;).nodes.filter(); var good = u(&#39;h2&#39;).nodes.some(); </code></pre> <p>However, there are also some advantages of using Umbrella&#39;s methods instead of native methods. For example, with <code>.addClass()</code> vs native <code>classList.add()</code>:</p> <ul> <li><strong>error prevention</strong>: if nodes.length = 0, the single-element way will fail in the above implementation (since first() and nodes[0] are null)</li> <li><strong>cross-browser</strong>: the classList.add() with multiple elements <a href="http://caniuse.com/#search=classList">is not compatible with IE10-11 &amp; Android 4.3-</a></li> <li><strong>chainable</strong>: <code>u(&#39;&lt;div&gt;&#39;).each(...).addClass(...);</code></li> <li><strong>more flexibility</strong>: there are many ways to specify multiple classes with addClass, and only one way to specify them on the native way. Imagine that you have an array of classes, with the native method this becomes a nightmare. This is what it means to be flexible:</li> </ul> <pre><code class="lang-js">u(&#39;h2&#39;).addClass(&#39;vanilla&#39;, &#39;test&#39;); // It accepts multiple parameters u(&#39;h2&#39;).addClass([&#39;vanilla&#39;, &#39;test&#39;]); // Also accept an array u(&#39;h2&#39;).addClass([&#39;vanilla&#39;], [&#39;test&#39;]); // Or multiple arrays u(&#39;h2&#39;).addClass(&#39;vanilla, test&#39;); // Strings with space and/or comma u(&#39;h2&#39;).addClass(&#39;vanilla&#39;, [&#39;test&#39;], &#39;one, more&#39; ); // Or just whatever </code></pre> <p>So it&#39;s convenient that you know these limitations and act accordingly. Try to use native methods where it makes sense, then Umbrella&#39;s methods where it&#39;s better suited or then create your own methods when you need it.</p> <h2 id="-length">.length</h2> <p>You can check how many elements are matched with <code>.length</code>:</p> <pre><code class="lang-js">// Check how many &lt;a&gt; are in the page alert(u(&#39;a&#39;).length); </code></pre> <h2 id="-addclass-">.addClass()</h2> <p>Add html class(es) to all of the matched elements.</p> <pre><code class="lang-js">.addClass(&#39;name1&#39;) .addClass(&#39;name1 name2 nameN&#39;) .addClass(&#39;name1,name2,nameN&#39;) .addClass(&#39;name1&#39;, &#39;name2&#39;, &#39;nameN&#39;) .addClass([&#39;name1&#39;, &#39;name2&#39;, &#39;nameN&#39;]) .addClass([&#39;name1&#39;, &#39;name2&#39;], [&#39;name3&#39;], [&#39;nameN&#39;]) .addClass(function(){ return &#39;name1&#39;; }) .addClass(function(){ return &#39;name1&#39;; }, function(){ return &#39;name2&#39;; }) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>name1</code>, <code>name2</code>, <code>nameN</code>: the class name (or variable containing it) to be added to all of the matched elements. It accepts many different types of parameters (see above).</p> <h3 id="return">Return</h3> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Add the class <code>main</code> to all the <code>&lt;h2&gt;</code> from the page:</p> <pre><code class="lang-js">u(&quot;h2&quot;).addClass(&quot;main&quot;); </code></pre> <p>Add the class <code>toValidate</code> and <code>ajaxify</code> to all the <code>&lt;form&gt;</code> present in the page:</p> <pre><code class="lang-js">u(&quot;form&quot;).addClass(&quot;toValidate&quot;, &quot;ajaxify&quot;); </code></pre> <h3 id="related">Related</h3> <p><a href="#hasclass">.hasClass()</a> finds if the matched elements contain the class(es).</p> <p><a href="#removeclass">.removeClass()</a> deletes class(es) from the matched elements.</p> <p><a href="#toggleclass">.toggleClass()</a> adds or removes the class</p> <h2 id="-after-">.after()</h2> <p>Add some html as a sibling after each of the matched elements.</p> <pre><code class="lang-js">.after(html) .after(&#39;&lt;div&gt;&#39;) .after(u(&#39;&lt;div&gt;&#39;)) .after(u(&#39;&lt;div&gt;&#39;).first()) // Same as document.createElement(&#39;div&#39;) .after(u(&#39;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&#39;)) .after(function(){}) .after(function(el){}, elements) .after(function(el){}, 10) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>html = &quot;&quot;</code>:</p> <ul> <li>Any of these elements:<ul> <li><strong>string</strong> containing the html that is going to be inserted</li> <li><strong>instance of Umbrella</strong></li> <li><strong>HTML node</strong></li> <li><strong>array</strong> containing HTML nodes</li> </ul> </li> <li>A callback that returns any of the previous. It gets passed these parameters:<ul> <li><strong>el</strong>: the current element from the elements parameter, {} if none is specified and i if elements is number</li> <li><strong>i</strong>: the index of the current element</li> </ul> </li> </ul> <p><code>elements = [{}]</code> (optional): It can be any of the following:</p> <ul> <li>An array of elements that will be passed to the callback. The callback is executed once per element, and all of them are added consecutively.</li> <li>A css selector, so the function will be executed once per matched element.</li> <li>A number, in which case the function will be executed that number of times</li> </ul> <h3 id="return">Return</h3> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Add a separator <code>&lt;hr&gt;</code> after each of the main titles h1:</p> <pre><code class="lang-js">u(&quot;h1&quot;).after(&quot;&lt;hr&gt;&quot;); </code></pre> <p>Add three elements after the link. All of these methods are equivalent:</p> <pre><code class="lang-js">// Add them all like a single string u(&quot;a.main&quot;).after(&quot;&lt;a&gt;One&lt;/a&gt;&lt;a&gt;Two&lt;/a&gt;&lt;a&gt;Three&lt;/a&gt;&quot;); // Add them in a chain u(&quot;a.main&quot;).after(&quot;&lt;a&gt;Three&lt;/a&gt;&quot;).after(&quot;&lt;a&gt;Two&lt;/a&gt;&quot;).after(&quot;&lt;a&gt;One&lt;/a&gt;&quot;); // Add them with a function parameter var cb = function(txt){ return &quot;&lt;a&gt;&quot; + txt + &quot;&lt;/a&gt;&quot; }; u(&quot;a.main&quot;).after(cb, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); // Same as the previous one but with ES6 u(&quot;a.main&quot;).after(txt =&gt; `&lt;a&gt;${ txt }&lt;/a&gt;`, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); </code></pre> <p>They all result in:</p> <pre><code class="lang-html">&lt;!-- previous data --&gt; &lt;a class=&quot;main&quot;&gt;&lt;/a&gt; &lt;a&gt;One&lt;/a&gt; &lt;a&gt;Two&lt;/a&gt; &lt;a&gt;Three&lt;/a&gt; </code></pre> <p>You can also add some events to them by creating an html node:</p> <pre><code class="lang-js">function greeting(){ alert(&quot;Hello world&quot;); } u(&quot;a.main&quot;).after(function(){ return u(&#39;&lt;a&gt;&#39;).addClass(&#39;hi&#39;).on(&#39;click&#39;, greeting).html(&quot;Greetings!&quot;); }); </code></pre> <h3 id="related">Related</h3> <p><a href="#before">.before()</a> Add some html before each of the matched elements.</p> <p><a href="#append">.append()</a> Add some html as a child at the end of each of the matched elements</p> <p><a href="#prepend">.prepend()</a> Add some html as a child at the beginning of each of the matched elements.</p> <h2 id="-ajax-">.ajax()</h2> <p>Make all of the matched forms to be submitted by ajax with the same action, method and values when the user submits the form.</p> <blockquote> <p>Note: this method does NOT submit the form, it just handles it when it&#39;s submitted (from the user or with .trigger())</p> </blockquote> <pre><code class="lang-js">.ajax(done, before); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>done</code> [optional]: A function to be called when the request ends. The first argument is the error, if any. The second is the body, which is parsed to JSON if it&#39;s a JSON string or just the body as a string if it&#39;s not JSON. The third is the request object itself.</p> <pre><code class="lang-js">var done = function(err, body, xhr){}; </code></pre> <p><code>before</code> [optional]: A function to be called before the request is sent. Useful to manipulate some data in real-time.</p> <pre><code class="lang-js">var before = function(xhr){}; </code></pre> <h3 id="return">Return</h3> <p><strong>Undefined</strong>. Please don&#39;t use the returned value for anything (it might be a promise in the future).</p> <h3 id="examples">Examples</h3> <p>Handle the newsletter through ajax</p> <pre><code class="lang-js">u(&#39;.newsletter&#39;).ajax(function(err){ if (err) return alert(&quot;Error&quot;); alert(&quot;Thank you for subscribing, awesome!&quot;); }); </code></pre> <p>Actually send a form through ajax:</p> <pre><code class="lang-js">u(&#39;form.edit&#39;).ajax(function(){ console.log(&#39;Sent!&#39;); }).trigger(&#39;submit&#39;); </code></pre> <h3 id="why-not-jquery-">Why not jquery?</h3> <p>This was created because this pattern is quite common in jquery:</p> <pre><code class="lang-js">$(&#39;form&#39;).on(&#39;submit&#39;, function(e){ e.preventDefault(); $.post($(this).attr(&#39;action&#39;), $(this).serialize(), function(data){ alert(&quot;Done! Thanks, &quot; + data.name); }, &#39;json&#39;); }); </code></pre> <p>After repeating that many times, I found out that it&#39;s better if we just make that the default. The same code on Umbrella JS:</p> <pre><code class="lang-js">u(&#39;form&#39;).ajax(function(err, data){ if (!err) alert(&#39;Done! Thanks, &#39; + data.name); }); </code></pre> <p>Of course you have freedom and you can use a similar method to jquery, but I think it&#39;s a bit pointless for this specific situation:</p> <pre><code class="lang-js">u(&#39;form&#39;).on(&#39;submit&#39;, function(e){ e.preventDefault(); var options = { method: u(this).attr(&#39;method&#39;), body: u(this).serialize() }; ajax(u(this).attr(&#39;action&#39;), options, function(err, data){ if (!err) alert(&quot;Done! Thanks, &quot; + data.name); }); }); </code></pre> <h3 id="related">Related</h3> <p><a href="#ajaxfn">ajax()</a>: perform ajax requests</p> <h2 id="-append-">.append()</h2> <p>Add some html as a child at the end of each of the matched elements</p> <pre><code class="lang-js">.append(html) .append(&#39;&lt;div&gt;&#39;) .append(u(&#39;&lt;div&gt;&#39;)) .append(u(&#39;&lt;div&gt;&#39;).first()) // Same as document.createElement(&#39;div&#39;) .append(u(&#39;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&#39;)) .append(function(){}) .append(function(el){}, elements) .append(function(el){}, 10) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>html = &quot;&quot;</code>:</p> <ul> <li>Any of these elements:<ul> <li><strong>string</strong> containing the html that is going to be inserted</li> <li><strong>instance of Umbrella</strong></li> <li><strong>HTML node</strong></li> <li><strong>array</strong> containing HTML nodes</li> </ul> </li> <li>A callback that returns any of the previous. It gets passed these parameters:<ul> <li><strong>el</strong>: the current element from the elements parameter, {} if none is specified and i if elements is number</li> <li><strong>i</strong>: the index of the current element</li> </ul> </li> </ul> <p><code>elements = [{}]</code> (optional): It can be any of the following:</p> <ul> <li>An array of elements that will be passed to the callback. The callback is executed once per element, and all of them are added consecutively.</li> <li>A css selector, so the function will be executed once per matched element.</li> <li>A number, in which case the function will be executed that number of times</li> </ul> <h3 id="return">Return</h3> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Add a footer to each of the articles</p> <pre><code class="lang-js">u(&quot;article&quot;).append(&quot;&lt;footer&gt;Hello world&lt;/footer&gt;&quot;); </code></pre> <p>Add three elements to the list. All of these methods are equivalent:</p> <pre><code class="lang-js">// Add them all like a single string u(&quot;ul&quot;).append(&quot;&lt;li&gt;One&lt;/li&gt;&lt;li&gt;Two&lt;/li&gt;&lt;li&gt;Three&lt;/li&gt;&quot;); // Add them in a chain u(&quot;ul&quot;).append(&quot;&lt;li&gt;One&lt;/li&gt;&quot;).append(&quot;&lt;li&gt;Two&lt;/li&gt;&quot;).append(&quot;&lt;li&gt;Three&lt;/li&gt;&quot;); // Add them with a function parameter var cb = function(txt){ return &quot;&lt;li&gt;&quot; + txt + &quot;&lt;/li&gt;&quot; }; u(&quot;ul&quot;).append(cb, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); // Same as the previous one but with ES6 u(&quot;ul&quot;).append(txt =&gt; `&lt;li&gt;${ txt }&lt;/li&gt;`, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); </code></pre> <p>They all result in:</p> <pre><code class="lang-html">&lt;ul&gt; &lt;!-- previous data --&gt; &lt;li&gt;One&lt;/li&gt; &lt;li&gt;Two&lt;/li&gt; &lt;li&gt;Three&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>You can also add some events to them by creating an html node:</p> <pre><code class="lang-js">function greet(){ alert(&quot;Hello world&quot;); } u(&quot;a.main&quot;).append(function(){ return u(&#39;&lt;a&gt;&#39;).addClass(&#39;hi&#39;).on(&#39;click&#39;, greet).html(&quot;Hey!&quot;); }); </code></pre> <h3 id="related">Related</h3> <p><a href="#prepend">.prepend()</a> Add some html as a child at the beginning of each of the matched elements.</p> <p><a href="#before">.before()</a> Add some html before each of the matched elements.</p> <p><a href="#after">.after()</a> Add some html as a sibling after each of the matched elements.</p> <h2 id="-array-">.array()</h2> <p>Extract structured data from the DOM.</p> <pre><code class="lang-js">.array() .array(callback) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>callback = function(node, i){ return node.innerHTML }</code>: a callback to be called on each node. The returned value is the one set on the final version. If an array is returned then these elements are added to the set. However, if nothing or null is returned it removes them.</p> <h3 id="return">Return</h3> <p>A simple javascript array consisting on the elements returned by the callback</p> <h3 id="example">Example</h3> <pre><code class="lang-html">&lt;ul&gt; &lt;li&gt;Peter&lt;/li&gt; &lt;li&gt;Mery&lt;/li&gt; &lt;li&gt;John&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Javascript (by default):</p> <pre><code class="lang-js">u(&#39;ul li&#39;).array(); // [&#39;Peter&#39;, &#39;Mery&#39;, &#39;John&#39;] </code></pre> <p>Javascript (with custom callback):</p> <pre><code class="lang-js">u(&#39;ul li&#39;).array(function(node){ return { name: u(node).text() }; }); // [{ name: &#39;Peter&#39; }, { name: &#39;Mery&#39; }, { name: &#39;John&#39; }] </code></pre> <h2 id="-attr-">.attr()</h2> <p>Handle attributes for the matched elements</p> <pre><code class="lang-js">// GET .attr(&#39;name&#39;); // SET .attr(&#39;name&#39;, &#39;value&#39;); .attr({ name1: &#39;value&#39;, name2: &#39;value2&#39; }); </code></pre> <h3 id="parameters">Parameters</h3> <p><em>GET</em></p> <p><code>name</code>: the attribute that we want to get from the first matched element</p> <p><em>SET</em></p> <p><code>name</code>: the attribute that we want to set for all of the matched elements</p> <p><code>value</code>: what we want to set the attribute to. If it&#39;s not defined, then we get the name</p> <h3 id="return">Return</h3> <p><em>GET</em></p> <p><code>string</code>: the value of the attribute</p> <p><em>SET</em></p> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="important">Important</h3> <p>You must understand that <code>.attr()</code> will only retrieve the attributes, not the properties like <code>checked</code>. To understand it better, check <a href="http://api.jquery.com/prop/">jquery&#39;s attr() vs prop()</a>.</p> <p>Each property is different so you should consult each case. For example, if you wanted to get the property <code>checked</code> you could do:</p> <pre><code class="lang-js">u(&#39;.terms-os-service&#39;).is(&#39;:checked&#39;); </code></pre> <h3 id="examples">Examples</h3> <p>Get the alt of an image:</p> <pre><code class="lang-js">u(&#39;img.hero&#39;).attr(&#39;alt&#39;); </code></pre> <p>Set the src of all of the images:</p> <pre><code class="lang-js">u(&#39;img&#39;).attr({ src: &#39;demo.jpg&#39; }); </code></pre> <h3 id="related">Related</h3> <p><a href="#data">.data()</a> handle data-* attributes for the matched elements</p> <h2 id="-before-">.before()</h2> <p>Add some html before each of the matched elements.</p> <pre><code class="lang-js">.before(html) .before(&#39;&lt;div&gt;&#39;) .before(u(&#39;&lt;div&gt;&#39;)) .before(u(&#39;&lt;div&gt;&#39;).first()) // Same as document.createElement(&#39;div&#39;) .before(u(&#39;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&#39;)) .before(function(){}) .before(function(el){}, elements) .append(function(el){}, 10) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>html = &quot;&quot;</code>:</p> <ul> <li>Any of these elements:<ul> <li><strong>string</strong> containing the html that is going to be inserted</li> <li><strong>instance of Umbrella</strong></li> <li><strong>HTML node</strong></li> <li><strong>array</strong> containing HTML nodes</li> </ul> </li> <li>A callback that returns any of the previous. It gets passed these parameters:<ul> <li><strong>el</strong>: the current element from the elements parameter, {} if none is specified and i if elements is number</li> <li><strong>i</strong>: the index of the current element</li> </ul> </li> </ul> <p><code>elements = [{}]</code> (optional): It can be any of the following:</p> <ul> <li>An array of elements that will be passed to the callback. The callback is executed once per element, and all of them are added consecutively.</li> <li>A css selector, so the function will be executed once per matched element.</li> <li>A number, in which case the function will be executed that number of times</li> </ul> <h3 id="return">Return</h3> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Add a header to each of the articles</p> <pre><code class="lang-js">u(&quot;article&quot;).after(&quot;&lt;header&gt;Hello world&lt;/header&gt;&quot;); </code></pre> <p>Add three elements before the link. All of these methods are equivalent:</p> <pre><code class="lang-js">// Add them all like a single string u(&quot;a.main&quot;).before(&quot;&lt;a&gt;One&lt;/a&gt;&lt;a&gt;Two&lt;/a&gt;&lt;a&gt;Three&lt;/a&gt;&quot;); // Add them in a chain u(&quot;a.main&quot;).before(&quot;&lt;a&gt;One&lt;/a&gt;&quot;).before(&quot;&lt;a&gt;Two&lt;/a&gt;&quot;).before(&quot;&lt;a&gt;Three&lt;/a&gt;&quot;); // Add them with a function parameter var cb = function(txt){ return &quot;&lt;a&gt;&quot; + txt + &quot;&lt;/a&gt;&quot; }; u(&quot;a.main&quot;).before(cb, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); // Same as the previous one but with ES6 u(&quot;a.main&quot;).before(txt =&gt; `&lt;a&gt;${ txt }&lt;/a&gt;`, [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;]); </code></pre> <p>They all result in:</p> <pre><code class="lang-html">&lt;a&gt;One&lt;/a&gt; &lt;a&gt;Two&lt;/a&gt; &lt;a&gt;Three&lt;/a&gt; &lt;a class=&quot;main&quot;&gt;&lt;/a&gt; &lt;!-- previous data --&gt; </code></pre> <p>You can also add some events to them by creating an html node:</p> <pre><code class="lang-js">function greeting(){ alert(&quot;Hello world&quot;); } u(&quot;a.main&quot;).before(function(){ return u(&#39;&lt;a&gt;&#39;).addClass(&#39;hi&#39;).on(&#39;click&#39;, greeting).html(&quot;Greetings!&quot;); }); </code></pre> <h3 id="related">Related</h3> <p><a href="#after">.after()</a> Add some html as a sibling after each of the matched elements.</p> <p><a href="#append">.append()</a> Add some html as a child at the end of each of the matched elements</p> <p><a href="#prepend">.prepend()</a> Add some html as a child at the beginning of each of the matched elements.</p> <h2 id="-children-">.children()</h2> <p>Get the direct children of all of the nodes with an optional filter</p> <pre><code class="lang-js">.children(filter); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>filter</code>: a string containing a selector that nodes must pass or a function that return a boolean. See <a href="#filter">.filter()</a> for a better explanation</p> <h3 id="return">Return</h3> <p><code>u</code>: returns an instance of Umbrella JS with the new children as nodes</p> <h3 id="examples">Examples</h3> <p>Get the first <code>&lt;li&gt;</code> of every <code>&lt;ul&gt;</code></p> <pre><code class="lang-js">u(&quot;ul&quot;).children(&#39;li:first-child&#39;); </code></pre> <h3 id="related">Related</h3> <p><a href="#parent">.parent()</a> get all of the direct parents</p> <p><a href="#find">.find()</a> get all of the descendants of the matched nodes</p> <p><a href="#closest">.closest()</a> get the first ascendant that matches the selector</p> <h2 id="-clone-">.clone()</h2> <p>Create a deep copy of the set of matched elements. Includes matched element node and <strong>all of its events</strong> as well as its children <strong>and all of their events</strong> by <strong>default</strong>.</p> <pre><code class="lang-js">u(&#39;.elementToClone&#39;).clone() </code></pre> <h3 id="extensions">Extensions</h3> <ul> <li>The following extensions are enabled by default:<ul> <li><strong>select</strong> select input node values are copied to all cloned nodes. To disable globally, add <code>u.prototype.mirror.select = false;</code> to your code.</li> <li><strong>textarea</strong> textarea input node values are copied to all cloned nodes. To disable globally, add <code>u.prototype.mirror.select = false;</code> to your code.</li> </ul> </li> </ul> <h3 id="return">Return</h3> <p><code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Clone a node and append to another.</p> <pre><code class="lang-html">&lt;div class=&quot;container&quot;&gt; &lt;div class=&quot;testClone1&quot;&gt;Hello&lt;/div&gt; &lt;div class=&quot;cloneDestination&quot;&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <pre><code class="lang-js">var clone = u(&quot;testClone1&quot;).clone(); u(&quot;.cloneDestination&quot;).append(clone); </code></pre> <p>Result:</p> <pre><code class="lang-html">&lt;div class=&quot;container&quot;&gt; &lt;div class=&quot;testClone1&quot;&gt;Hello&lt;/div&gt; &lt;div class=&quot;cloneDestination&quot;&gt; &lt;div class=&quot;testClone1&quot;&gt;Hello&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <h3 id="related">Related</h3> <p><a href="#append">.append()</a> add some html as a child at the end of each of the matched elements.</p> <h2 id="-closest-">.closest()</h2> <p>Find the first ancestor that matches the selector for each node</p> <pre><code class="lang-js">.closest(filter); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>filter</code>: a string containing a selector that nodes must pass or a function that return a boolean. See <a href="#filter">.filter()</a> for a better explanation</p> <h3 id="return">Return</h3> <p><code>u</code>: returns an instance of Umbrella JS with the new ancestors as nodes</p> <h3 id="examples">Examples</h3> <p>Get the ul of every li</p> <pre><code class="lang-js">u(&quot;li&quot;).closest(&#39;ul&#39;); </code></pre> <h3 id="related">Related</h3> <p><a href="#find">.find()</a> get all of the descendants of the matched nodes</p> <p><a href="#parent">.parent()</a> get all of the direct parents</p> <p><a href="#children">.children()</a> get the direct children of all of the nodes with an optional filter</p> <h2 id="-data-">.data()</h2> <p>Handle data-* attributes for the matched elements</p> <pre><code class="lang-js">// GET .data(&#39;name&#39;); // SET .data(&#39;name&#39;, &#39;value&#39;); .data({ name1: &#39;value&#39;, name2: &#39;value2&#39; }); </code></pre> <h3 id="parameters">Parameters</h3> <p><em>GET</em></p> <p><code>name</code>: the data-* attribute that we want to get from the first matched element</p> <p><em>SET</em></p> <p><code>name</code>: the data-* attribute that we want to set for all of the matched elements</p> <p><code>value</code>: what we want to set the attribute to. If it&#39;s not defined, then we get the name</p> <h3 id="return">Return</h3> <p><em>GET</em></p> <p><code>string</code>: the value of the data-* attribute</p> <p><em>SET</em></p> <p><code>u</code>: data-* returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Get the value for data-id:</p> <pre><code class="lang-html">&lt;ul&gt; &lt;li data-id=&#39;0&#39;&gt;First&lt;/li&gt; &lt;li data-id=&#39;1&#39;&gt;Second&lt;/li&gt; &lt;li data-id=&#39;2&#39;&gt;Third&lt;/li&gt; &lt;/ul&gt; </code></pre> <pre><code class="lang-js">u(&#39;ul li&#39;).first().data(&#39;id&#39;); // 0 </code></pre> <p>Set the data-id of an element:</p> <pre><code class="lang-js">u(&#39;ul li&#39;).first().data({ id: &#39;1&#39; }); // &lt;li data-id=&#39;1&#39;&gt;First&lt;/li&gt; u(&#39;ul li&#39;).first().data(&#39;id&#39;, &#39;2&#39;); // &lt;li data-id=&#39;2&#39;&gt;First&lt;/li&gt; </code></pre> <h3 id="related">Related</h3> <p><a href="#attr">.attr()</a> handle attributes for the matched elements</p> <h2 id="-each-">.each()</h2> <p>Loop through all of the nodes and execute a callback for each</p> <pre><code class="lang-js">.each(function(node, i){}); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>callback</code>: the function that will be called. It accepts two parameters, the node and the index. <code>this</code> is Umbrella&#39;s instance so other methods like <code>this.args()</code> and <code>this.slice()</code> are available.</p> <h3 id="return">Return</h3> <p><code>u</code>: returns an instance of Umbrella JS with the same nodes</p> <h3 id="examples">Examples</h3> <p>Loop through all of the links and add them a <code>target=&quot;_blank&quot;</code>:</p> <pre><code class="lang-js">u(&#39;a&#39;).each(function(node, i){ u(node).attr({ target: &#39;_blank&#39; }); }); </code></pre> <h2 id="-empty-">.empty()</h2> <p>Remove all child nodes of the matched elements.</p> <pre><code class="lang-js">.empty(); </code></pre> <h3 id="parameters">Parameters</h3> <p>This method doesn&#39;t accept any parameters</p> <h3 id="return">Return</h3> <p><code>u</code>: Returns an instance of Umbrella JS with the empty nodes.</p> <h3 id="examples">Examples</h3> <p>Removes all child nodes from all containers:</p> <pre><code class="lang-js">u(&quot;div.container&quot;).empty(); </code></pre> <h3 id="related">Related</h3> <p><a href="#remove">.remove()</a> Removes the matched elements.</p> <h2 id="-filter-">.filter()</h2> <p>Remove all the nodes that doesn&#39;t match the criteria</p> <pre><code class="lang-js">.filter(&#39;a&#39;) .filter(u(&#39;a&#39;)) .filter(function(node, i){ return u(node).is(&#39;a&#39;); }) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>filter</code>: it can be:</p> <ul> <li>css selector that each of the nodes must match to stay</li> <li>instance of umbrella with the elements to keep (the intersection will be kept)</li> <li>function that returns a boolean with true to keep the element. It accepts two parameters, <code>node</code> and <code>index</code>, and the context of <code>this</code> is the instance of umbrella so methods like <code>this.slice()</code> are available</li> </ul> <h3 id="returns">Returns</h3> <p>An instance of Umbrella with the nodes that passed the filter.</p> <h3 id="examples">Examples</h3> <p>Get only the active links</p> <pre><code class="lang-js">var links = u(&#39;a&#39;).filter(&#39;.active&#39;); </code></pre> <p>Get all of the paragraphs with a link:</p> <pre><code class="lang-js">var paragraphs = u(&#39;p&#39;).filter(function(node){ return u(node).find(&#39;a&#39;).length &gt; 0; }); </code></pre> <p>Get only the inputs with an answer above 5 and show an error:</p> <pre><code class="lang-js">u(&#39;input&#39;).filter(function(node, i){ if (parseInt(u(node).first().value) &gt; 5) { return true; } }).addClass(&#39;error&#39;); </code></pre> <h3 id="related">Related</h3> <p><a href="#is">.is()</a> check whether one or more of the nodes is of one type</p> <p><a href="#not">.not()</a> remove all the nodes that match the criteria</p> <h2 id="-find-">.find()</h2> <p>Get all of the descendants of the nodes with an optional filter</p> <pre><code class="lang-js">.find(filter); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>filter</code>: a string containing a selector that nodes must pass or a function that return a boolean. See <a href="#filter">.filter()</a> for a better explanation</p> <h3 id="return">Return</h3> <p>An instance of Umbrella with the new children as nodes</p> <h3 id="examples">Examples</h3> <p>Get all of the links within all the paragraphs</p> <pre><code class="lang-js">u(&quot;p&quot;).find(&#39;a&#39;); </code></pre> <p>Get the required fields within a submitting form:</p> <pre><code class="lang-js">u(&#39;form&#39;).on(&#39;submit&#39;, function(e){ var required = u(this).find(&#39;[required]&#39;); }); </code></pre> <h3 id="related">Related</h3> <p><a href="#closest">.closest()</a> get the first ascendant that matches the selector</p> <p><a href="#parent">.parent()</a> get all of the direct parents</p> <p><a href="#find">.children()</a> get the direct child of the matched nodes</p> <h2 id="-first-">.first()</h2> <p>Retrieve the first of the matched nodes</p> <pre><code class="lang-js">.first(); </code></pre> <h3 id="parameters">Parameters</h3> <p>This method doesn&#39;t accept any parameters</p> <h3 id="return">Return</h3> <p>The first html node or false if there is none.</p> <h3 id="examples">Examples</h3> <p>Retrieve the first element of a list:</p> <pre><code class="lang-js">var next = u(&quot;ul.demo li&quot;).first(); </code></pre> <h3 id="related">Related</h3> <p><a href="#last">.last()</a> retrieve the last matched element</p> <h2 id="ajax-fn">ajax() fn</h2> <p>Function (not method) that allows performing ajax requests. The implementation is somewhat similar to <a href="https://github.com/yanatan16/nanoajax">nanoajax</a>:</p> <pre><code class="lang-js">var action = &#39;/save&#39;; var options = { body: &#39;a=b&#39; }; var after = function(err, data){ console.log(data); }; var before = function(xhr){}; ajax(action, options, after, before); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>action</code>: the place where to send the ajax request</p> <p><code>options</code>: an object that sets the options to be passed. These are:</p> <ul> <li><code>method = &#39;GET&#39;</code>: the way to send the request. It can be GET or POST</li> <li><code>body = &#39;&#39;</code>: a string on the <code>a=b&amp;c=d</code> format or a simple object that will be converted</li> <li><code>headers = {}</code>: an object with <code>{ key: value }</code> headers to be manually set</li> </ul> <p><code>after</code>: the callback to be called when the request has been sent and parsed. The first parameter is an error that can be null, and the second one the parsed data in JSON or the unparsed data as an string.</p> <p><code>before</code>: a callback that can be called just before sending the request. It receives the XHR object as the first parameter.</p> <h3 id="return">Return</h3> <p>Returns the already sent XHR object.</p> <h3 id="tips">Tips</h3> <p>You can modify the XHR object straight by using the <em>before</em> callback. It is called just before sending the request, after setting all its parameters:</p> <pre><code class="lang-js">ajax(&#39;/save&#39;, {}, after, function(xhr){ xhr.responseType = &#39;json&#39;; }); </code></pre> <h2 id="-handle-">.handle()</h2> <p>This function is the same as <a href="#on"><code>on()</code></a>, but it executes the <code>e.preventDefault()</code> so you don&#39;t need to do it. So these two are exactly the same:</p> <pre><code class="lang-js">u(&#39;form.login&#39;).on(&#39;submit&#39;, function(e){ e.preventDefault(); // logic }); </code></pre> <pre><code class="lang-js">u(&#39;form.login&#39;).handle(&#39;submit&#39;, function(e){ // logic }); </code></pre> <h3 id="related">Related</h3> <p><a href="#on">.on()</a> Calls a function when an event is triggered</p> <p><a href="#trigger">.trigger()</a> calls an event on all of the matched nodes</p> <p><a href="#off">.off()</a> Removes an event from matched nodes</p> <h2 id="-hasclass-">.hasClass()</h2> <p>Find if any of the matched elements contains the class passed:</p> <pre><code class="lang-js">.hasClass(&#39;name1&#39;); .hasClass(&#39;name1 name2 nameN&#39;); .hasClass(&#39;name1,name2,nameN&#39;); .hasClass(&#39;name1&#39;, &#39;name2&#39;, &#39;nameN&#39;); .hasClass([&#39;name1&#39;, &#39;name2&#39;, &#39;nameN&#39;]); .hasClass([&#39;name1&#39;, &#39;name2&#39;], [&#39;name3&#39;], [&#39;nameN&#39;]); .hasClass(function(){ return &#39;name1&#39;; }); .hasClass(function(){ return &#39;name1&#39;; }, function(){ return &#39;name2&#39;; }); </code></pre> <p>If more than one class is passed, they are checked <strong>with the AND condition</strong> similar to:</p> <pre><code class="lang-js">u(&quot;a&quot;).hasClass(&quot;button&quot;) &amp;&amp; u(&quot;a&quot;).hasClass(&quot;primary&quot;); </code></pre> <h3 id="parameters">Parameters</h3> <p><code>name1</code>, <code>name2</code>, <code>nameN</code>: the class name (or variable containing it) to be matched to any of the matched elements. It accepts many different types of parameters (see above).</p> <h3 id="return">Return</h3> <p><strong><code>boolean</code></strong>: returns true if all of the passed classes are found in any of the matched elements and false if they couldn&#39;t be found.</p> <h3 id="example">Example</h3> <p>You can also check manually if it has several classes with the OR parameter with:</p> <pre><code class="lang-js">u(&#39;a&#39;).is(&#39;.button, .primary&#39;); </code></pre> <p>And with the AND parameter:</p> <pre><code class="lang-js">u(&#39;a&#39;).is(&#39;.button.primary&#39;); </code></pre> <p>Toggle the color of a button depending on the status</p> <pre><code class="lang-html">&lt;a class=&quot;example button&quot;&gt;Click me&lt;/a&gt; &lt;script src=&quot;//umbrellajs.com/umbrella.min.js&quot;&gt;&lt;/script&gt; &lt;script&gt; u(&quot;.example&quot;).on(&#39;click&#39;, function() { if(u(this).hasClass(&quot;error&quot;)) { u(this).removeClass(&quot;error&quot;).html(&quot;Click me&quot;); } else { u(this).addClass(&quot;error&quot;).html(&quot;Confirm&quot;); } }); &lt;/script&gt; </code></pre> <h3 id="related">Related</h3> <p><a href="#addclass">.addClass()</a> adds html class(es) to each of the matched elements.</p> <p><a href="#removeclass">.removeClass()</a> deletes class(es) from the matched elements.</p> <h2 id="-html-">.html()</h2> <p>Retrieve or set the html of the elements:</p> <pre><code class="lang-js">// GET .html(); // SET .html(html); </code></pre> <h3 id="parameters">Parameters</h3> <p><em>GET</em> should pass no parameter so it retrieves the html.</p> <p><em>SET</em> <code>html</code>: the new value that you want to set. To remove it, pass an empty string: <code>&quot;&quot;</code></p> <h3 id="return">Return</h3> <p><em>GET</em> <code>string</code>: the html of the first node</p> <p><em>SET</em> <code>u</code>: returns the same instance of Umbrella JS</p> <h3 id="examples">Examples</h3> <p>Get the main title:</p> <pre><code class="lang-js">var title = u(&#39;h1&#39;).html(); </code></pre> <p>Set the main title:</p> <pre><code class="lang-js">u(&#39;h1&#39;).html(&#39;Hello world&#39;); </code></pre> <h3 id="related">Related</h3> <p><a href="#attr">.text()</a> Retrieve or set the textContent of the elements</p> <p><a href="#attr">.attr()</a> Handle attributes for the matched elements</p> <h2 id="-is-">.is()</h2> <p>Check whether any of the nodes matches the selector</p> <pre><code class="lang-js">.is(&#39;a&#39;) .is(u(&#39;a&#39;)) .is(function(){ return Math.random() &gt; 0.5 }) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>filter</code>: it can be two things:</p> <ul> <li>css selector to check</li> <li>instance of umbrella with the elements to check</li> <li>function that returns a boolean to check for each of the nodes. If one of them returns true, then the method <code>is()</code> returns true. It accepts two parameters, <code>node</code> and <code>index</code>, and the context of <code>this</code> is the instance of umbrella so methods like <code>this.slice()</code> are available.</li> </ul> <h3 id="return">Return</h3> <p><em>boolean</em>: <em>true</em> if any of the nodes matches the selector or the function returns true, false otherwise.</p> <h3 id="examples">Examples</h3> <p>Check if the current form needs to be valdated</p> <pre><code class="lang-js">u(&#39;form.subscribe&#39;).ajax(false, function() { // Same as u(&#39;form.subscribe&#39;).hasClass(&#39;validate&#39;) if (u(&#39;form.subscribe&#39;).is(&#39;.validate&#39;)) { validate(); } }); </code></pre> <h3 id="related">Related</h3> <p><a href="#filter">.filter()</a> remove unwanted nodes</p> <p><a href="#not">.not()</a> remove all the nodes that match the criteria</p> <h2 id="-last-">.last()</h2> <p>Get the last element from a list of elements.</p> <pre><code class="lang-js">.last(); </code></pre> <h3 id="parameters">Parameters</h3> <p>This method doesn&#39;t accept any parameters</p> <h3 id="return">Return</h3> <p>The last html node or false if there is none.</p> <h3 id="examples">Examples</h3> <p>Retrieve the last element of a list:</p> <pre><code class="lang-js">var next = u(&quot;ul.demo li&quot;).last(); </code></pre> <h3 id="related">Related</h3> <p><a href="#first">.first()</a> retrieve the first matched element</p> <h2 id="-map-">.map()</h2> <p>Change the content of the current instance by looping each element</p> <pre><code class="lang-js">.map(function(){}); </code></pre> <h3 id="parameters">Parameters</h3> <p>A single callback that returns the element(s) that are going to be kept:</p> <pre><code class="lang-js">var links = u(&#39;.special li&#39;).map(function(node, i){ if (parseInt(node.innerHTML) &gt; 10) { return &#39;&lt;a&gt;&#39; + u(node).data(&#39;id&#39;) + &#39;&lt;/a&gt;&#39;; } }).addClass(&#39;expensive&#39;); </code></pre> <p>It can return a value that evaluates to false, a single element, an string, an array or an Umbrella instance. It will <strong>remove duplicated nodes</strong> from the result.</p> <blockquote> <p>Note: Umbrella JS is made to manipulate HTML nodes so it will consider the string &quot;&quot; and 0 as false and remove them. Return an HTML node or an HTML string to keep the elements.</p> </blockquote> <h3 id="return">Return</h3> <p>An instance of Umbrella with the nodes passed</p> <h3 id="examples">Examples</h3> <p>Get the parent elements (see <a href="#parent">.parent()</a>):</p> <pre><code class="lang-js">var lists = u(&#39;li&#39;).map(function(node){ return node.parentNode }); </code></pre> <h3 id="related">Related</h3> <p><a href="#each">.each()</a> loop all the elements without changing them</p> <h2 id="-not-">.not()</h2> <p>Remove known nodes from nodes</p> <pre><code class="lang-js">.not(&#39;a&#39;) .not(u(&#39;a&#39;)) .not(function(node){ return Math.random() &gt; 0.5; }) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>not</code>: it can be two things (in order):</p> <ul> <li>css selector that each of the nodes must <strong>not</strong> match to stay</li> <li>instance of umbrella with the element to remove</li> <li>function that returns <code>true</code> to remove the element. It accepts <strong>one parameter</strong>, and the context of <code>this</code> is the instance of umbrella so methods like <code>this.slice()</code> are available</li> </ul> <pre><code class="lang-js">.not(function(node){ // your code }); </code></pre> <h3 id="examples">Examples</h3> <pre><code class="lang-html">&lt;ul class=&quot;menu&quot;&gt; &lt;li&gt;&lt;a class=&quot;active&quot;&gt;Menu item 1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a&gt;Menu item 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a&gt;Menu item 3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Get only the non-active links on paragraphs</p> <pre><code class="lang-js">var nonactive_links = u(&#39;.menu a&#39;).not(&#39;.active&#39;); </code></pre> <p>Get all of the active:</p> <pre><code class="lang-js">active_links = u(&#39;.menu a&#39;).not(nonactive_links); </code></pre> <h3 id="related">Related</h3> <p><a href="#is">.is()</a> check whether one or more of the nodes is of one type</p> <p><a href="#filter">.filter()</a> Remove unwanted nodes</p> <h2 id="-off-">.off()</h2> <p>Remove event handler from matched nodes</p> <pre><code class="lang-js">.off(&#39;event1&#39;) .off(&#39;event1 event2 eventN&#39;) .off(&#39;event1,event2,eventN&#39;) .off([&#39;event1&#39;, &#39;event2&#39;, &#39;eventN&#39;]) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>event</code>: Any number of events (such as click, mouseover)</p> <p><code>listener</code>: Function reference to remove from the events</p> <h3 id="examples">Examples</h3> <pre><code class="lang-html">&lt;ul&gt; &lt;li class=&quot;off-single-test&quot;&gt;1&lt;/li&gt; &lt;li class=&quot;off-multiple-test&quot;&gt;2&lt;/li&gt; &lt;li class=&quot;off-multiple-test&quot;&gt;3&lt;/li&gt; &lt;/ul&gt; </code></pre> <pre><code class="lang-js">const listener = function() { alert(&#39;called&#39;); } //Add listener u(&#39;.off-multiple-test&#39;).on(&#39;click&#39;, listener); //Trigger event u(&#39;.off-multiple-test&#39;).trigger(&#39;click&#39;); //Alert appears //Remove listener u(&#39;.off-multiple-test&#39;).off(&#39;click&#39;, listener); //Trigger event u(&#39;.off-multiple-test&#39;).trigger(&#39;click&#39;); //No alert </code></pre> <h3 id="related">Related</h3> <p><a href="#on">.on()</a> Attaches an event to matched nodes</p> <p><a href="#handle">.handle()</a> Same as <code>.on()</code>, but it prevents the default action</p> <p><a href="#trigger">.trigger()</a> Triggers an event on all of the matched nodes</p> <h2 id="-on-">.on()</h2> <p>Calls a function when an event is triggered</p> <pre><code class="lang-js">.on(&#39;event1&#39;, callback) .on(&#39;event1 event2 eventN&#39;, callback) .on(&#39;event1,event2,eventN&#39;, callback) .on([&#39;event1&#39;, &#39;event2&#39;, &#39;eventN&#39;], callback) .on(&#39;event1&#39;, &#39;selector&#39;, callback) </code></pre> <h3 id="parameters">Parameters</h3> <p><code>event1</code>, <code>event2</code>, <code>eventN</code>: the name(s) of the events to listen for actions, such as <code>click</code>, <code>submit</code>, <code>change</code>, etc.</p> <p><code>callback</code>: function that will be called when the event is triggered. The parameters it accepts are <code>function(e, data1, data2, ..., dataN)</code>:</p> <ul> <li><p><code>e</code>: the event that was triggered. It has some interesting properties:</p> <ul> <li><code>e.currentTarget</code>: Contains the element that triggered the event.</li> <li><code>e.preventDefault()</code>: Avoids the browser from performing the default action.</li> <li><code>e.details</code>: an array of the argument data passed to <code>trigger()</code> if it was passed with that function. See other arguments:</li> </ul> </li> <li><p><code>data1</code>, <code>data2</code>, <code>dataN</code>: the arguments that were passed to <code>trigger()</code> if it was called with that function.</p> </li> </ul> <p>Another way is doing event delegation, for which the parameters are:</p> <p><code>event1</code>, <code>event2</code>, <code>eventN</code>: same as before</p> <p><code>selector</code>: a css selector that matches the nodes that will trigger it</p> <p><code>callback</code>: same as before</p> <h3 id="return">Return</h3> <p>Umbrella instance</p> <h3 id="examples">Examples</h3> <p>An auto-save feature that submits the form through ajax every 10 seconds</p> <pre><code class="lang-js">// Show &#39;test&#39; when the button test is clicked u(&#39;button.test&#39;).on(&#39;click&#39;, function(e) { alert(&quot;Test&quot;); }); // This example is very similar to .ajax() implementation u(&#39;form.tes