-
Notifications
You must be signed in to change notification settings - Fork 101
/
docs.html
1581 lines (1193 loc) · 59.1 KB
/
docs.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Bliss.js — Documentation</title>
<link rel="shortcut icon" href="style/twitter-avatar.png" />
<link rel="stylesheet" href="style/prism.css" />
<link rel="stylesheet" href="style/style.css" />
<link rel="stylesheet" href="style/docs.css" />
</head>
<body class="language-javascript" id="docs">
<header>
<a href="index.html">
<h1>Bliss</h1>
<p>Heavenly JavaScript</p>
</a>
</header>
<nav id="toc">
<h1>Contents</h1>
<ol></ol>
</nav>
<section id="basics">
<h1>Overview</h1>
<p>Bliss includes several static methods (on the <code>Bliss</code> or <code>$</code> object). For example, to copy all properties of an object onto another object, you would call <a href="$.extend"><code>$.extend()</code></a>:</p>
<pre><code>var yolo = $.extend({foo: 1}, {bar: 2}); // or Bliss.extend(…)
// yolo is {foo: 1, bar: 2}</code></pre>
<p>Many of Bliss’ methods take an element or an array of elements as their first argument. For example, to set both the <code>width</code> and <code>padding</code> of an element to 0, you could use the <a href="#$.style"><code>$.style()</code></a> method:</p>
<pre><code>$.style(element, {width: 0, padding: 0});</code></pre>
<p>These types of methods are also available on elements and arrays, for convenience. However, since adding convenience methods to elements and arrays directly would be a JS Mortal Sin™, Bliss only <a href="http://lea.verou.me/2015/04/idea-extending-native-dom-prototypes-without-collisions">adds a <code>_</code> property on elements & arrays</a>, on which it hangs all its methods, to avoid conflicts. The previous example would be written as:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
});</code></pre>
<aside><p>The <code>._.</code> sequence of characters that appears all too often when coding with Bliss is where Bliss gets its logo from. However, <a href="#configuration">the property can be customized to anything you want</a>.</p></aside>
<p>Methods that are available on elements like this will have an <a class="tag element">Element</a> tag in these docs.</p>
<p>If you wanted to set the width and padding of multiple elements to 0, you could use an array:</p>
<pre><code>myArray._.style({
"width": 0,
"padding": 0
});</code></pre>
<p>Methods that are available on arrays like this will have an <a class="tag array">Array</a> tag in these docs.</p>
<p>For example, assume that in addition to these CSS changes you also wanted to add a <code>hidden</code> attribute to this element. Bliss methods that don’t return a value return the element or array they are called on, so you could call more element methods on them, including native ones:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
}).setAttribute("hidden", "");</code></pre>
<p>Now assume you also wanted to set a second attribute: The "class" attribute to "foo". The native <code>setAttribute()</code> method is not chainable, it returns <code>undefined</code>. However, all native element methods are also available on the almighty <code>_</code> property, and there they are also chainable:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
})._.setAttribute("hidden", "").setAttribute("class", "foo");</code></pre>
<p>This works, but it’s a bit unwieldy. Thankfully, Bliss offers an <code>$.attributes()</code> method for setting multiple attributes at once:</p>
<pre><code>element._.style({
"width": 0,
"padding": 0
})._.attributes({
"hidden": "",
"class": "foo"
});</code></pre>
<p>This is better and more readable, but still a bit awkward. Turns out there is a special <a href="#$.set"><code>$.set()</code></a> method to do both at once:</p>
<pre><code>element._.set({
attributes: {
"hidden": "",
"class": "foo"
},
style: {
"width": 0,
"padding": 0
}
});</code></pre>
<p>Because <code>$.attributes()</code> and <code>$.style()</code> are also available for <code>$.set()</code> parameters, they will have the special <a class="tag set">$.set()</a> tag in these docs.</p>
<p>Note that you don’t actually need <code>attributes: {…}</code> in <code>$.set()</code> at all: if there are any unrecognized properties in the parameter object, Bliss will first check if there is a property with that name on the element and if not, set an attribute. So you could rewrite the example above as:</p>
<pre><code>element._.set({
"hidden": "",
"class": "foo", // or "className": "foo" to use the property
style: {
"width": 0,
"padding": 0
}
});</code></pre>
</section>
<section id="vanilla">
<h1>Vanilla Methods</h1>
<p>You don’t need Bliss or any library for any of these. All the following are 100% pure Vanilla JS! Click on the code for documentation and browser support info.</p>
<table>
<thead>
<tr>
<th>Action</th>
<th>Vanilla JS</th>
</tr>
</thead>
<tbody>
<tr>
<td>Adding the class "my-class" on <code>element</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.add("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Removing the class "my-class" from <code>element</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.remove("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Adding the class "my-class" on <code>element</code> if it doesn’t already have it and removing it if it does:</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.toggle("my-class");</code></pre></a></td>
</tr>
<tr>
<td>Checking if <code>element</code> contains the class "my-class"</td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList"><pre><code>element.classList.contains("my-class")</code></pre></a>
</td>
</tr>
<tr>
<td>Removing <code>element</code> from the DOM</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove"><pre><code>element.remove();</code></pre></a></td>
</tr>
<tr>
<td>Checking if <code>element</code> contains <code>otherElement</code></td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/contains"><pre><code>element.contains(otherElement)</code></pre></a>
</td>
</tr>
<tr>
<td>Check if <code>element</code> matches <code>selector</code></td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/matches">
<pre><code>element.matches(selector)</code></pre>
</a></td>
</tr>
<tr>
<td>Find the closest ancestor (or self) of <code>element</code> that matches <code>selector</code></td>
<td>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/closest">
<pre><code>element.closest(selector)</code></pre>
</a>
</td>
</tr>
<tr>
<td>Find next sibling of <code>element</code> that is a real element (not a text node or comment node):</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling">
<pre><code>element.nextElementSibling</code></pre>
</a></td>
</tr>
<tr>
<td>Get all children of <code>element</code> which are real elements</td>
<td><a href="https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/children">
<pre><code>element.children</code></pre>
</a></td>
</tr>
<!-- tr>td+td>a[href]>pre>code -->
</tbody>
</table>
<p>Note that all vanilla <strong>methods</strong> from elements (specifically, from <code>HTMLElement</code>) are also available on <code>$</code> as well as the <code>_</code> property for both elements and arrays, so for example, to remove all divs on a page, you could write either of the following:</p>
<pre><code>$$("div")._.remove();</code></pre>
<pre><code>$.remove($$("div"));</code></pre>
</section>
<section>
<h1>DOM</h1>
<p><strong>Note:</strong> The <code>$</code> is just an alias to <code>Bliss</code>. If another library that uses <code>$</code> is included before Bliss, then Bliss will not use <code>$</code>. You can always alias <code>$</code> to <code>Bliss</code> manually, by putting this before any of your code (but after including Bliss):</p>
<pre><code>self.$ = Bliss;</code></pre>
<p>Similarly, <code>$$()</code> is an alias to Bliss’ <code>$.$()</code> or <code>Bliss.$()</code> function. If it’s already defined by the time Bliss is called, Bliss will not use it. You can do so manually with this line:</p>
<pre><code>self.$$ = Bliss.$;</code></pre>
<p>When writing Bliss plugins, if you want to use <code>$()</code> and <code>$$()</code> it’s good practice to make local variables with them, in case they’re not available:</p>
<pre><code>(function($, $$){
// Plugin code here
})(Bliss, Bliss.$)</code></pre>
<article>
<h1 class="transform-ignore">$()</h1>
<p class="description">Select an element by selector. Mainly a shortcut for <code>element.querySelector()</code>.</p>
<pre><code>var element = $(selector [, context])</code></pre>
<dl class="args">
<dt class="string">selector</dt>
<dd>The CSS selector to use.</dd>
<dt class="element">context</dt>
<dd>Returned element needs to be a descendant of this element. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Ascope"><code>:scope</code></a> inside <code>selector</code> refers to this element (for supporting browsers).</dd>
<dt class="element">element</dt>
<dd>The matched element or <code>null</code> if none found.</dd>
</dl>
<pre class="examples"><code>// return the first element with a class of .foo
// that is inside the first element with a class of .bar
var ret = $(".foo", $(".bar"));</code></pre>
<pre><code>// Return the first element with a class of .foo
// that is inside any element with a class of .bar
var ret = $(".bar .foo");</code></pre>
<pre><code>// Get the first element with a class of .foo
// and set its title attribute to "yolo"
// If there is no such element, this will throw an exception!
$(".foo").setAttribute("title", "yolo");</code></pre>
<ul class="notes">
<li>If the element is not found, it will return <code>null</code>. This could cause an exception if methods are called on the result.
You might previously check if the element exists by caching the node in a variable
into an <code>if</code> statement:</li>
</ul>
<pre><code>// Check if the .foo element exists
// and set an attribute by using a variable "foo"
// as a reference of the element
var foo = $(".foo");
if (foo) {
foo.setAttribute("title", "yolo");
}</code></pre>
<a href="http://api.jquery.com/jQuery/#jQuery1" class="jq">$</a>
</article>
<article>
<h1 class="transform-ignore">$$()</h1>
<p class="description">Get all elements that match a given selector as an array. Similar to <code>element.querySelectorAll()</code> but returns an Array instead of a NodeList, so it has all of the convenience methods we’ve come to love on arrays.</p>
<pre><code>var array = $$(selector [, context])</code></pre>
<dl class="args">
<dt class="string">selector</dt>
<dd>The CSS selector to use.</dd>
<dt class="element">context</dt>
<dd>Returned elements need to be a descendants of this element. <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Ascope"><code>:scope</code></a> inside <code>selector</code> refers to this element (for supporting browsers).</dd>
<dt class="array">array</dt>
<dd>The matched elements as an array.</dd>
</dl>
<pre class="def"><code>var array = $$(collection)</code></pre>
<dl class="args">
<dt>collection</dt>
<dd class="type">Type: Array-like</dd>
<dd>Any array-like object, such as a NodeList, a function’s <code>arguments</code> object or an elements <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes"><code>element.attributes</code></a> collection.</dd>
<dt class="array">array</dt>
<dd>The collection converted to an array.</dd>
</dl>
<pre class="examples"><code>// Add an id to all <h1> headings that don’t already have one
$$("h1:not([id])").forEach(function(h1){
h1.id = h1.textContent.replace(/\W/g, "");
});</code></pre>
<pre><code>// Get an array with all ids on the page
var ids = $$("[id]").map(function(element){
return element.id;
});</code></pre>
<pre><code>// Get all of an element’s attributes starting with data-bliss-
$$(element.attributes).filter(function(attribute){
return attribute.name.indexOf("data-bliss-") === 0;
}).map(function(attribute){
return attribute.name;
});</code></pre>
</article>
<article>
<h1>create</h1>
<p class="description">Create an element.</p>
<pre><code>var element = $.create([tag,] [options]);</code></pre>
<dl class="args">
<dt class="string">tag</dt>
<dd>The tag name of the element to create.</dd>
<dt class="object">options</dt>
<dd>An object with several options for the new element (properties, attributes, events etc). For details about what options this object accepts, see <code>$.set()</code>. If <code>tag</code> is omitted, you need to provide it in the <code>options</code> object.</dd>
<dt class="element">element</dt>
<dd>The newly created element.</dd>
</dl>
<pre><code>$.create("ul", {
className: "nav",
contents: [
"Navigation: ",
{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
},
{tag: "li",
contents: {tag: "a",
href: "contact.html",
textContent: "Contact",
target: "_blank"
}}
]
});</code></pre>
<pre><code>var paragraph = $.create("p");</code></pre>
<pre><code>var div = $.create();</code></pre>
<ul class="notes">
<li>If you’re only creating an element without setting any options on it, just use the native <code>document.createElement(tag)</code></li>
<li><code>$.create(tag, options)</code> is equivalent to:
<pre><code>$.set(document.createElement(tag), options)</code></pre> This is also the recommended syntax if you need a different <code>document</code> than the current one.</li>
</ul>
</article>
<article>
<h1 class="element array">set</h1>
<p class="description">Set multiple traits of an element (properties, attributes, events, contents etc). One of the most useful functions in Bliss.</p>
<pre><code>subject = $.set(subject, options);</code></pre>
<pre><code>subject = subject._.set(options);</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element to apply the options on.</dd>
<dt class="object">options</dt>
<dd>An object with various options, including:
<dl>
<dt>*</dt>
<dd>Any remaining properties will be added as properties or attributes on the element (Bliss first checks if there is a property with that name, and if not, it sets it as an attribute.</dd>
</dl>
</dd>
</dl>
<pre><code>$.set(document.createElement("nav"), {
style: {
color: "red"
},
events: {
click: function(evt) {
console.log("YOLO");
}
},
contents: ["Navigation: ", {tag: "ul",
className: "buttons",
delegate: {
click: {
li: function() {
console.log("A list item was clicked");
}
}
},
contents: [{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
}, {tag: "li",
contents: {tag: "a",
href: "docs.html",
textContent: "Docs"
}
}
]
}],
inside: $("body > header")
});</code></pre>
<ul class="notes">
<li><strong>Tip:</strong> Add more specially handled properties of your own by just adding a function to <code>$.setProps</code>.</li>
<li><strong>Warning:</strong> If setting SVG attributes, use the explicit <code>attributes</code> property, not just properties directly on <code>options</code>. Otherwise, they will be added as properties, and the API for SVG DOM properties is the stuff of nightmares.</li>
</ul>
</article>
<article>
<h1 class="element array set">contents</h1>
<p class="description">Append existing or newly created element(s) and text nodes to an element.</p>
<pre><code>subject = $.contents(subject, elements)</code></pre>
<pre><code>subject = subject._.contents(elements)</code></pre>
<pre><code>subject = subject._.set({contents: elements})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to append the elements to.</dd>
<dt class="object array string number node">elements</dt>
<dd>If it’s an object, it will be passed through <code>$.create()</code>, to be converted into an element. If it’s a string or number, it will be converted to a text node. If it’s a <code>Node</code> it will be used as-is. If it’s an array, its elements will be processed according to the aforementioned rules. Note that this lets you recursively create entire subtrees, since these objects can also have a <code>contents</code> property of their own, as seen in the example below.</dd>
</dl>
<pre><code>nav._.contents(["Navigation: ", {tag: "ul",
className: "buttons",
delegate: {
click: {
li: function() {
console.log("A list item was clicked")
}
}
},
contents: [{tag: "li",
contents: {tag: "a",
href: "index.html",
textContent: "Home"
}
}, {tag: "li",
contents: {tag: "a",
href: "docs.html",
textContent: "Docs"
}
}
]
}])</code></pre>
<a class="jq">jQuery.fn.html</a>
</article>
<article class="full">
<h1 class="element full">clone</h1>
<p class="description">Clone an element including its descendants, with events and data. <strong>This function is deprecated and will be removed in the next version of Bliss</strong></p>
<pre><code>var clone = $.clone(subject)</code></pre>
<pre><code>var clone = subject._.clone()</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be cloned.</dd>
<dt class="element">clone</dt>
<dd>The cloned element.</dd>
</dl>
<pre><code>var button = $("button");
button.addEventListener("click", function() { console.log("Click from listener!"); });
button.onclick = function() { console.log("Click from inline event!"); };
var button2 = button._.clone();
// If clicked, button2 will print both messages</code></pre>
<ul class="notes">
<li>If you don’t care about copying events and data, just use the native <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode"><code>element.cloneNode()</code></a></li>
</ul>
<a class="jq">jQuery.fn.clone</a>
</article>
<article>
<h1 class="element array set">after</h1>
<p class="description">Insert an element after another.</p>
<pre><code>subject = $.after(subject, element)</code></pre>
<pre><code>subject = subject._.after(element)</code></pre>
<pre><code>subject = subject._.set({after: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be inserted.</dd>
<dt class="element">element</dt>
<dd>The element to insert after.</dd>
</dl>
<p class="needs example"></p>
<a class="jq">jQuery.fn.after</a>
</article>
<article>
<h1 class="element set">around</h1>
<p class="description">Wrap an element around another.</p>
<pre><code>subject = $.around(subject, element)</code></pre>
<pre><code>subject = subject._.around(element)</code></pre>
<pre><code>subject = subject._.set({around: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to wrap with.</dd>
<dt class="element">element</dt>
<dd>The element to be wrapped.</dd>
</dl>
<pre><code>// Wrap headings with a link to their section
$$("section[id] > h1, article[id] > h1").forEach(function(h1){
$.create("a", {
href: "#" + h1.parentNode.id,
around: h1
});
});</code></pre>
<a class="jq">jQuery.fn.wrap</a>
</article>
<article>
<h1 class="element array set">attributes</h1>
<p class="description">Set multiple attributes on one or more elements.</p>
<pre><code>subject = $.attributes(subject, attrs)</code></pre>
<pre><code>subject = subject._.attributes(attrs)</code></pre>
<pre><code>subject = subject._.set({attributes: attrs})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the attributes on.</dd>
<dt class="object">attrs</dt>
<dd>An object with the attributes and values to set.</dd>
</dl>
<p class="needs example"></p>
<ul class="notes">
<li>If only setting one attribute on one element, consider using the native <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute"><code>element.setAttribute(name, value)</code></a>. If setting one attribute on multiple elements, you can use <code>myArray._.setAttribute(name, value)</code>.</li>
</ul>
</article>
<article>
<h1 class="element array">toggleAttribute</h1>
<p class="description">Set or remove an attribute based on a test</p>
<pre><code>subject = $.toggleAttribute(subject, name, value [, test])</code></pre>
<pre><code>subject = subject._.toggleAttribute(attrs)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the attributes on.</dd>
<dt class="string">name</dt>
<dd>The attribute name</dd>
<dt class="string">value</dt>
<dd>The attribute value</dd>
<dt>test</dt>
<dd>The test. If truthy, set the attribute. If falsy, remove it. If not present, it’s false when <code>value</code> is <code>null</code> and true otherwise.</dd>
</dl>
<p class="needs example"></p>
</article>
<article>
<h1 class="element array set">before</h1>
<p class="description">Insert the element before another.</p>
<pre><code>subject = $.before(subject, element)</code></pre>
<pre><code>subject = subject._.before(element)</code></pre>
<pre><code>subject = subject._.set({before: element})</code></pre>
<dl class="args">
<dt class="element">subject</dt>
<dd>The element to be inserted.</dd>
<dt class="element">element</dt>
<dd>The element to insert before.</dd>
</dl>
<p class="needs examples"></p>
<a class="jq">jQuery.fn.before</a>
</article>
<article>
<h1 class="element array set">inside</h1>
<p class="description">Insert the element inside another, after any existing contents.</p>
<pre><code>subject = $.inside(subject, element)</code></pre>
<pre><code>subject = subject._.inside(element)</code></pre>
<pre><code>subject = subject._.set({inside: element})</code></pre>
<ul class="notes">Mainly useful inside <code>$.set()</code>, otherwise just use the native <code>element.appendChild(subject)</code>.</ul>
<a class="jq">jQuery.fn.append</a>
</article>
<article>
<h1 class="element array set">properties</h1>
<p class="description">Set a number of properties on an element.</p>
<pre><code>subject = $.properties(subject, props)</code></pre>
<pre><code>subject = subject._.properties(props)</code></pre>
<pre><code>subject = subject._.set({properties: props})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the properties on.</dd>
<dt class="object">props</dt>
<dd>The properties to set on the element.</dd>
</dl>
<pre><code>document.createElement("button")._.properties({
className: "continue",
textContent: "Next Step",
onclick: function() { MyApp.next() }
});</code></pre>
<ul class="notes">
<li>If only setting one property on one element, just use <code>element[property] = value;</code>. If setting multiple properties on one element, you can use <code>$.extend(subject, props)</code> instead.</li>
</ul>
<a class="jq">jQuery.fn.prop</a>
</article>
<article>
<h1 class="element array set">start</h1>
<p class="description">Insert an element inside another, before its existing contents.</p>
<pre><code>subject = $.start(subject, element)</code></pre>
<pre><code>subject = subject._.start(element)</code></pre>
<pre><code>subject = subject._.set({start: element})</code></pre>
<a class="jq">jQuery.fn.prepend</a>
</article>
<article>
<h1 class="element array set">style</h1>
<p class="description">Set multiple CSS properties on one or more elements. Both camelCase and hyphen-case attributes are allowed.</p>
<pre><code>subject = $.style(subject, properties)</code></pre>
<pre><code>subject = subject._.style(properties)</code></pre>
<pre><code>subject = subject._.set({style: properties})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to apply the CSS properties to.</dd>
<dt class="object">properties</dt>
<dd>The CSS properties to apply, in camelCase format</dd>
</dl>
<pre class="examples"><code>document.body._.style({
color: "white",
backgroundColor: "red",
cssFloat: "left",
"--my-variable": 5
});</code></pre>
<ul class="notes">
<li>If only setting one property on one element, you don’t need this. Just use the native <code>element.style.propertyName = value;</code> syntax.</li>
</ul>
<a class="jq">jQuery.fn.css</a>
</article>
<article>
<h1 class="element">transition</h1>
<p class="description">Set multiple CSS properties with a transition and execute code after the transition is finished.</p>
<pre><code>var promise = $.transition(subject, properties [, duration])</code></pre>
<pre><code>var promise = subject._.transition(properties [, duration])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to apply the transitions to.</dd>
<dt class="object">properties</dt>
<dd>The CSS properties to apply, in camelCase format</dd>
<dt class="number">duration</dt>
<dd>The duration, in milliseconds. Defaults to 400.</dd>
<dt class="promise">promise</dt>
<dd>Promise that gets resolved after the transition is done, or immediately <a href="http://caniuse.com/#feat=css-transitions">if CSS transitions are not supported</a>.</dd>
</dl>
<pre class="examples"><code>// Fade out an element then remove it from the DOM
$.transition(element, {opacity: 0}).then($.remove);</code></pre>
<pre><code>// Fade out and shrink all <div>s on a page,
// then remove them from the DOM
Promise.all($$("div")._.transition({
opacity: 0,
transform: "scale(0)"
})).then($.remove);</code></pre>
<ul class="notes">
<li>Uses CSS transitions, not custom interpolation code. In browsers that do not support CSS transitions, the style is applied immediately and the promise returned is resolved.</li>
<li>The properties applied remain after the transition has ended.</li>
</ul>
<a class="jq">jQuery.fn.animate</a>
</article>
</section>
<section>
<h1>Events</h1>
<article>
<h1 class="element array set">delegate</h1>
<p class="description">Helper for event delegation. Helps you register events for children at an ancestor, so that when you add children that match the provided selector, they also trigger the event.</p>
<pre><code>subject = $.delegate(subject, type, selector, callback)</code></pre>
<pre><code>subject = subject._.delegate(type, selector, callback)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="string">selector</dt>
<dd>The selector the target of the event needs to match.</dd>
</dl>
<pre class="def"><code>subject = $.delegate(subject, type, selectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.delegate(type, selectorsToCallbacks)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="object">selectorsToCallbacks</dt>
<dd>An object of the form <code>{selector1: callback1, …, selectorN: callbackN}</code>.</dd>
</dl>
<pre class="def"><code>subject = $.delegate(subject, typesToSelectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.delegate(typesToSelectorsToCallbacks)</code></pre>
<pre class="def"><code>subject = subject._.set({delegate: typesToSelectorsToCallbacks})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The delegation root. Only elements inside this will fire the event.</dd>
<dt class="object">typesToSelectorsToCallbacks</dt>
<dd>An object of the form <pre><code>{
type1: {selector11: callback11, …, selector1N: callback1N},
…
typeM: {selectorM1: callbackM1, …, selectorMK: callbackMK}
}</code></pre>.</dd>
</dl>
<a class="jq">jQuery.fn.delegate</a>
</article>
<article>
<h1 class="element array">bind</h1>
<p class="description">Set multiple event listeners on one or more elements.</p>
<pre><code>subject = $.bind(subject, handlers)</code></pre>
<pre><code>subject = subject._.bind(handlers)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object">handlers</dt>
<dd>An object whose keys are the events and the values the listeners.
For details on the keys, see <code>types</code> below.
For details on the values, see <code>callback</code> and <code>options</code> below.</dd>
</dl>
<pre><code>$$('input[type="range"]')._.bind({
"input change": function(evt) { this.title = this.value},
"focus.className": function(evt) { console.log(evt.type); }
})</code></pre>
<pre class="def"><code>subject = $.bind(subject, types, [callback], [options])</code></pre>
<pre class="def"><code>subject = subject._.bind(types, [callback], [options])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="string">types</dt>
<dd>The event type. You can include multiple event types by space-separating them.
You can add a “class” on each event type by using a period (<code>click.myclass</code>), which you can later use in <code>$.unbind()</code> for unbinding all events with a certain class at once.</dd>
<dt class="function">callback</dt>
<dd>The function to execute. Passed directly to <code>addEventListener</code>.</dd>
<dt class="boolean object">options</dt>
<dd>Capture or options object to pass to <code>addEventListener</code>.
For details on what options are available, refer to the documentation for <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener"><code>addEventListener</code></a>
You can also combine the <code>options</code> and <code>callback</code> parameters, by including a <code>callback</code> property.
This is useful when using the syntax above for multiple handlers if you also want to pass options.
</dd>
</dl>
<ul class="notes">
<li>If only setting one event listener on one element and you are keeping a reference to the callback (or are using Bliss Full), just use the native <code>element.addEventListener(type, handler, useCapture)</code> syntax.
</li>
<li>You can use classes in your event names, like <code>click.foo</code>. The event that will be bound in that case is without the class name (<code>click</code>), but you can use the class name to unbind via <code>$.unbind()</code></li>
</ul>
<a class="jq">jQuery.fn.bind</a>
</article>
<article>
<h1 class="element array set">events</h1>
<p class="description">Just like <code>$.bind()</code>, but also lets you copy events from another element.
The latter is the only syntax described here, for the <code>$.bind()</code> syntax look above.</p>
<pre><code>subject = $.events(subject, element)</code></pre>
<pre><code>subject = subject._.events(element)</code></pre>
<pre><code>subject = subject = subject._.set({events: element})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object element">element</dt>
<dd>The element to copy listeners from. Inline events set via HTML or <code>on*</code> properties are also copied.</dd>
</dl>
<ul class="notes">
<li>If using Bliss Shy, only listeners added via Bliss methods will be copied.</li>
</ul>
</article>
<article>
<h1 class="element array">unbind</h1>
<p class="description">Unbind event listeners en masse.</p>
<pre><code>subject = $.unbind(subject, handlers)</code></pre>
<pre><code>subject = subject._.unbind(handlers)</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to unbind the event listeners from.</dd>
<dt class="object">handlers</dt>
<dd>An object whose keys are the event types and the values the callbacks.
For details about what these can be, read about the <code>type</code> and <code>callback</code> arguments below.</dd>
</dl>
<pre><code>$$('input[type="range"]')._.unbind({
"input change": function(evt) { this.title = this.value},
})</code></pre>
<pre class="def"><code>subject = $.unbind(subject, [type[, callback]])</code></pre>
<pre class="def"><code>subject = subject._.unbind([type[, callback]])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to unbind the event listeners from.</dd>
<dt class="string">type</dt>
<dd>The event type. You can include multiple event types by space-separating them.
<strong>If using Bliss Full</strong>:
You can also unbind by class (e.g. <code>click.foo</code> or even just <code>.foo</code> to unbind all events with that class).
You can also unbind all listeners on the element, by not providing <strong>any</strong> arguments. This will remove all listeners with capture option set to either true or false.
This works for Bliss Shy and anonymous functions too, if the listeners were added using Bliss bind method. Bliss (both Full and Shy) keeps track of anonymous functions so these listeners may be removed later.
</dd>
<dt class="function">callback</dt>
<dd>The callback. <strong>If using Bliss Full</strong>, this is optional. If not provided, all callbacks matching the event type and/or class specified by <code>type</code> will be unbound.</dd>
</dl>
<pre><code>document.body._.events({
"click.foo mousedown.foo": function(e) { console.log("foo", e.type)},
"click.bar mousedown.bar": function(e) { console.log("bar", e.type)},
});
// Clicking on the body now prints foo mousedown, bar mousedown, foo click, bar click
document.body._.unbind(".foo");
// Clicking on the body now prints bar mousedown, bar click
document.body._.unbind("click");
// Clicking on the body now prints only bar mousedown
document.body._.unbind(); // unbind ALL events
// Clicking on the body now does nothing
$.unbind(elem)
// Removing all listeners from elem, regardless of event capture setting.
// If using Bliss Shy, $.unbind only works for the listeners added using $.bind method
$.unbind(elem, '', false)
// Removing all listeners from elem, with capture option set to false (the default in most browsers)
$.unbind(elem, '', true)
// Removing all listeners from elem, with capture option set to true explicitly
</code></pre>
<ul class="notes">
<li>If only unbinding one event listener and you have a reference to the callback used, just use the native <code>element.removeEventListener(type, handler, useCapture)</code> syntax.
</li>
<li>If using Bliss Shy, only listeners bound via Bliss methods can be removed without a reference to the callback.</li>
<li>Avoid using overly liberal unbinding queries (e.g. no arguments or just a type) in code meant to be used in environments you don’t control.
You don’t want to conflict with other people’s code! Use a class instead.</li>
</ul>
<a class="jq">jQuery.fn.unbind</a>
</article>
<article>
<h1 class="element array">fire</h1>
<p class="description">Fire a synthesized event.</p>
<pre><code>subject = $.fire(subject, type [, properties])</code></pre>
<pre><code>subject = subject._.fire(type [, properties])</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to fire the synthesized event on.</dd>
<dt class="string">type</dt>
<dd>The event type.</dd>
<dt class="object">properties</dt>
<dd>If provided, these properties will be added to the event object.</dd>
</dl>
<pre class="examples"><code>// Fire a custom event on a map widget
myMap._.fire("locationchange", {
location: [42.361667, -71.092751]
});</code></pre>
<pre><code>// Fire a fake input event
myInput._.fire("input");</code></pre>
<ul class="notes">
<li>If you are writing a library meant to be run on websites you don’t control, it is recommended that you namespace your events by adding the library name before them, like <code>"mylibrary-locationchange"</code> or <code>"mylibrary:locationchange"</code>, to avoid collisions.</li>
</ul>
<a class="jq">jQuery.fn.trigger</a>
</article>
<article>
<h1 class="element array set">once</h1>
<p class="description">Set event listeners that will be fired once per callback per element.</p>
<pre><code>subject = $.once(subject, handlers)</code></pre>
<pre><code>subject = subject._.once(handlers)</code></pre>
<pre><code>subject = subject = subject._.set({once: handlers})</code></pre>
<dl class="args">
<dt class="element array">subject</dt>
<dd>The element(s) to set the event listeners on.</dd>
<dt class="object element">handlers</dt>
<dd>An object whose keys are the events and the values the listeners. You can include multiple event types by space-separating them.</dd>
</dl>
<a class="jq">jQuery.fn.one</a>
</article>
<article>
<h1>ready</h1>
<p class="description">Execute code after the DOM is ready.</p>
<pre><code>var promise = $.ready([context], [callback])</code></pre>
<dl class="args">
<dt class="document">context</dt>
<dd>The document whose …readiness we’re interested in.</dd>
<dt class="function">callback</dt>
<dd>A callback that gets executed synchronously if the DOM is already ready or on <code>DOMContentLoaded</code> if not.</dd>
<dt class="promise">promise</dt>
<dd>A promise that gets resolved immediately if the DOM is already ready, or on <code>DOMContentLoaded</code> if not.</dd>
</dl>