public
Description: Miscellaneous small jQuery plugins
Homepage: http://onehackoranother.com/projects/jquery/jquery-grab-bag/
Clone URL: git://github.com/jaz303/jquery-grab-bag.git
jquery-grab-bag / dom-builder.html
100644 121 lines (101 sloc) 4.872 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
  <head>
    <title>jQuery Grab Bag - DOM Builder Plugin</title>
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://onehackoranother.com/feed/" />
    <link rel="alternate" type="text/xml" title="RSS .92" href="http://onehackoranother.com/feed/rss/" />
    <link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="http://onehackoranother.com/feed/atom/" />
    <script type='text/javascript' src='javascripts/jquery-1.2.6.min.js'></script>
    <script type='text/javascript' src='javascripts/jquery.dom-builder.js'></script>
    <link rel="stylesheet" href="stylesheets/main.css" type="text/css" />
    <style type='text/css'>
      #foo { background-color: #cdcdcd; }
      td, th { padding: 0.2em; }
      th { font-weight: bold; }
        .col-1 { color: red; }
        .col-2 { color: green; }
        .col-3 { color: blue; }
    </style>
    <script type='text/javascript'>
      $(function() {
        $$('table#foo',
          $$('tr', $$('th.col-1', 'ID'), $$('th.col-2', 'Username'), $$('th.col-3', 'Email')),
          $$('tr', $$('td', 1), $$('td', 'jaz303'), $$('td', 'jason@magiclamp.co.uk')),
          { style: "margin:20px auto",
            hover: [
              function() { $(this).css('backgroundColor', '#efefef'); },
              function() { $(this).css('backgroundColor', '#cdcdcd'); }
            ]
          }
        ).appendTo('#test-container');
        
        TABLE(
          TR( TH('This is'), TH('another table') ),
          TR( TD('generated using'), TD('shortcut methods') ),
          {style: "margin:20px auto"}
        ).appendTo('#test-container-2');
        
      });
    </script>
  </head>
  <body>
    <div id='container'>
      
      <h1>
        grab bag <span class='subtitle'>- assorted jQuery plugins</span>
      </h1>
      
      <div id='sidebar'>
        <ul id='project-nav'>
          <li><a href='index.html'>Plugin List</a></li>
        </ul>
        <ul id='ohoa-nav'>
          <li><a href='http://onehackoranother.com/feed/' id='subscribe'>Subscribe for updates &raquo;</a></li>
          <li><a href='http://onehackoranother.com/projects/'>Back to projects &raquo;</a></li>
          <li><a href='http://onehackoranother.com/'>Back to onehackoranother.com &raquo;</a></li>
        </ul>
      </div>
      
      <div id='main'>
        
        <h2>DOM Builder</h2>
        
        <p>
          This plugin defines the double-dollar function which can be used to create
          DOM node hierarchies, set attributes, hook up event handlers and return the
          resulting structure as a jQuery object. Probably best illustrated with some
          code:
        </p>
        
        <div class='caption'>Example:</div>
        <pre>$$('table#foo',
  $$('tr', $$('th.col-1', 'ID'), $$('th.col-2', 'Username'), $$('th.col-3', 'Email')),
  $$('tr', $$('td', 1), $$('td', 'jaz303'), $$('td', 'jason@magiclamp.co.uk')),
  { style: "margin:20px auto",
    hover: [
      function() { $(this).css('backgroundColor', '#efefef'); },
      function() { $(this).css('backgroundColor', '#cdcdcd'); }
    ]
  }
).appendTo('#test-container');</pre>
        
        <p>And here's the resulting table (it's auto-generated, trust me):</p>
      
        <div id='test-container'></div>
        
        <p>It's also possible to use shortcut methods for each tag although by using
          these you forgo the ability to neatly specify IDs/classes:
          
        <div class='caption'>Example:</div>
        <pre>TABLE(
  TR( TH('This is'), TH('another table') ),
  TR( TD('generated using'), TD('shortcut methods') ),
  {style: "margin:20px auto"}
).appendTo('#test-container-2');</pre>
        
        <div id='test-container-2'></div>
        
        <p>The general form of each call to <code>$$()</code> is:</p>
        
        <pre>$$(nodeName, content1, content2 ... contentN, options);</pre>
        
        <p>
          This will create a node with the given <code>nodeName</code>, apply to it
          the given options (if any) and then append the content, each of which may
          be a string, jQuery object or DOM element. Options are objects of key-value
          pairs; function values will be bound to the event denoted by the key, otherwise
          the corresponding attribute will be set. The <code>hover</code> option is
          special; when set to an array, it is assumed to contain two functions to handle
          hover in/out events on the element.
        </p>
    
        <h3>Direct Link</h3>
        <p><a href='http://github.com/jaz303/jquery-grab-bag/tree/master/javascripts/jquery.dom-builder.js'>jquery.dom-builder.js</a></p>
        
      </div>
      
    </div>
  </body>
</html>