Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>A Plugin</h2> <p>Below is a plugin I wrote that allows you to create an element, simply adorn it with attributes, and append it to the DOM. Here's an example use of <code>.adorn()</code> with what you wanted to do:</p> <pre><code>$("&lt;div&gt;").adorn("#errors",".red","&gt;body"); </code></pre> <p>The above creates a <code>div</code>, slaps on an <code>ID</code> and a <code>Class</code> and appends it all to the <code>body</code>. Note that you can add the <code>appendTo</code> syntax in any order, so this will work too:</p> <pre><code>$("&lt;div&gt;").adorn("&gt;body","#errors",".red"); </code></pre> <h2><strong><a href="http://jsfiddle.net/TathN/" rel="nofollow noreferrer">Simple example page created with adorn.</a></strong></h2> <p><br/></p> <p>As opposed to using one continuos string, I found it easier to pass each class, id, value, etc. in as an argument, since that clearly delineates them. <br/><br/></p> <h2>Syntax:</h2> <ul> <li><code>.blah</code> - adds class <code>blah</code> </li> <li><code>#blah</code> - sets id to <code>blah</code></li> <li><code>&gt;blah</code> - appends this to <code>blah</code> </li> <li><code>h:blah</code> - sets innerHTML to <code>blah</code> </li> <li><code>v:blah</code> - sets value to <code>blah</code> </li> <li>There's 9 options currently </li> <li>... you can easily add new functionality.</li> </ul> <p><em>Note that when used, the colon, can be any single character, it doesn't have to be a colon, so <code>h-blah</code> would also work.</em> <br/><br/></p> <h2>Other Uses:</h2> <p>The nice thing about this plugin is that it cannot only be used to adorn newly created elements, but it can also be used to adorn existing elements. For example to add 3 classes and set the HTML for all divs on a page:</p> <pre><code>$("div").adorn(".one",".two",".three","h:blah"); </code></pre> <h2><strong><a href="http://jsfiddle.net/xQ9z6/" rel="nofollow noreferrer">jsFiddle example</a></strong></h2> <p><br/></p> <p>Finally, if you accidentally pass in the wrong label. <strong><a href="http://jsfiddle.net/rwMm6/" rel="nofollow noreferrer">An error class is added to the element</a></strong> to ease debugging, but things will not break. <br/><br/></p> <h2>How it Works:</h2> <p>The heart of this plugin is making use of the automatically available <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments" rel="nofollow noreferrer">arguments array</a></strong> to hold all the passed in options. The options are parsed using a <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/switch" rel="nofollow noreferrer">switch statement</a></strong> and the <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/switch" rel="nofollow noreferrer">substring method</a></strong>. <br/><br/></p> <h2>The plugin:</h2> <pre><code>(function( $ ){ jQuery.fn.adorn = function () { // Get the arguments array for use in the closure var $arguments = arguments; // Maintain chainability return this.each(function () { var $this = $(this); $.each($arguments, function(index, value) { // These are just the options that quickly came // to mind, you can easily add more. // Which option is used depends on the first // character of the argument passed in. The 2nd // char is sometimes used and sometimes ignored. switch (value.substring(0,1)) { case "#": $this.attr("id",value.substring(1)); break; case ".": $this.addClass(value.substring(1)); break; case "&gt;": $this.appendTo(value.substring(1)); break; case "a": $this.attr("alt", value.substring(2)); break; case "h": $this.html(value.substring(2)); break; case "i": $this.attr("title", value.substring(2)); break; case "s": $this.attr("src", value.substring(2)); break; case "t": $this.attr("type", value.substring(2)); break; case "v": $this.attr("value", value.substring(2)); break; default: // if the wrong key was entered, create // an error class. $this.addClass("improper-adorn-label"); } }); }); }; })( jQuery ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload