forked from thednp/bootstrap.native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
3188 lines (2818 loc) · 185 KB
/
index.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>
<!--[if IE 7]><html class="ie ie7" lang="en"><![endif]-->
<!--[if IE 8]><html class="ie ie8" lang="en"><![endif]-->
<!--[if IE 9]><html class="ie ie9" lang="en"><![endif]-->
<!--[if gte IE 10 | !IE ]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="The Bootstrap sweetest native JavaScript library for vanilla JavaScript lovers.">
<meta name="keywords" content="JavaScript,Native JavaScript,native,Bootstrap,plugins,jQuery">
<meta name="author" content="dnp_theme">
<title>Native JavaScript for Bootstrap</title>
<!-- Bootstrap 3 core CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- local testing
<script src="./assets/js/html5shiv.min.js"></script>
<link href="./assets/css/bootstrap.min.css" rel="stylesheet"> -->
<!-- Bootstrap 3 CSS for IE8 -->
<!--[if lt IE 9]><link href="./assets/css/bootstrap-ie8.min.css" rel="stylesheet"><![endif]-->
<!-- Custom styles for this template -->
<link href="./assets/css/theme.css" rel="stylesheet">
<!-- Synthax highlighter -->
<link href="./assets/css/prism.css" rel="stylesheet">
<link rel="shortcut icon" href="./assets/img/favicon.ico">
<link rel="apple-touch-icon" href="./assets/img/apple-touch-icon.png">
<!-- The automated browser upgrade service provided by Financial Times.
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,getComputedStyle|gated"></script> -->
<!-- Just use it for all browsers on production websites. -->
<!-- Native JavaScript for Bootstrap 3 Polyfill -->
<!-- <script src="https://cdn.jsdelivr.net/bootstrap.native/2.0.13/polyfill.min.js"></script> -->
<script src="./dist/polyfill.js"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar-wrapper">
<div class="navbar navbar-inverse navbar-fixed-top bg-bootstrap" role="navigation">
<div class="navbar-header">
<button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#bs-navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://github.com/thednp/bootstrap.native/" data-toggle="tooltip" data-placement="bottom" class="navbar-brand" title="<b>Native JavaScript for Bootstrap</b><br>The sweetest Vanilla JavaScript library for everyone">
<svg class="d-inline-block align-top" width="36" height="36" viewBox="0 0 612 612" xmlns="http://www.w3.org/2000/svg" focusable="false"><title>Bootstrap</title>
<path fill="currentColor" d="M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z"></path><path fill="currentColor" d="M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46.8 0 72.29-18.68 72.29-53 0-31.42-21.53-48.78-60-48.78h-75.57zm78.22 145.46c47.68 0 72.73-19.34 72.73-56s-25.93-55.37-76.46-55.37h-74.49v111.4z"></path>
</svg>
<span class="hidden-xs d-inline-block align-middle">Native JavaScript for Bootstrap</span>
</a>
</div>
<nav id="bs-navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="v4.html" data-toggle="tooltip" title="Switch to V4">V4</a></li>
<li class="dropdown"><a href="#" data-toggle="dropdown" aria-expanded="false">Github <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/thednp/bootstrap.native/">Fork</a></li>
<li><a href="https://github.com/thednp/bootstrap.native/wiki">Wiki</a></li>
<li><a href="https://github.com/thednp/bootstrap.native/archive/master.zip">Download</a></li>
</ul>
</li>
<li class="dropdown"><a href="#" data-toggle="dropdown" aria-expanded="false">Share <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a target="_blank" rel="noreferrer" href="https://www.facebook.com/sharer/sharer.php?u=http://thednp.github.io/bootstrap.native/index.html" title="Share Native JavaScript for Bootstrap on Facebook">Facebook</a></li>
<li>
<a target="_blank" rel="noreferrer"
href="https://twitter.com/home?status=Spread the word about %23BootstrapNative by @dnp_theme and download here http://thednp.github.io/bootstrap.native/index.html"
title="Share Native JavaScript for Bootstrap on Twitter">Twitter</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-keyboard="true" data-pause="hover" data-interval="7000">
<ol class="carousel-indicators">
<li data-toggle="tooltip" data-title="Download Native JavaScript for Bootstrap" data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-toggle="tooltip" data-title="Getting started with Native JavaScript" data-target="#myCarousel" data-slide-to="1"></li>
<li data-toggle="tooltip" data-title="HTML5 inside" data-target="#myCarousel" data-slide-to="2"></li>
<li data-toggle="tooltip" data-title="Why BSN?" data-target="#myCarousel" data-slide-to="3"></li>
<li data-toggle="tooltip" data-title="Scroll To Carousel" data-target="#myCarousel" data-slide-to="4"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<div class="item-bg bg-primary"></div>
<div class="container">
<div class="carousel-caption slide">
<h2>A better way to Bootstrap</h2>
<p>The jQuery plugins for Bootstrap 3 redeveloped with native JavaScript, providing same basic functionality, but lighter in size and delivering higher performance for your application.</p>
<p><a class="btn btn-lg btn-default btn-empty" href="https://github.com/thednp/bootstrap.native/archive/master.zip">Download</a> <a class="btn btn-lg btn-info" rel="noreferrer" href="https://github.com/thednp/bootstrap.native/wiki" target="_blank" role="button">Wiki</a></p>
</div>
</div>
</div>
<div class="item">
<div class="item-bg bg-pink"></div>
<div class="container">
<div class="carousel-caption">
<h2>New to native JavaScript?</h2>
<p>Have no worry, the internet is full of scripts and tutorials, <a href="http://vanilla-js.com/" rel="noreferrer" target="_blank">native JavaScript</a> is the coolest programing language ever! Far more powerful and
requires almost <b>zero</b> maintenance on very long periods of time.</p>
<p><a class="btn btn-lg btn-default btn-empty" href="http://blog.garstasio.com/you-dont-need-jquery/" rel="noreferrer" target="_blank" role="button">Get started</a>
<a class="btn btn-lg btn-default btn-empty" href="http://jstherightway.org" role="button" rel="noreferrer" target="_blank">Full Reference</a></p>
</div>
</div>
</div>
<div class="item">
<div class="item-bg bg-indigo"></div>
<div class="container">
<div class="carousel-caption">
<h2>HTML5 inside</h2>
<p>This library have been developed for today's standards, for a workflow careless about legacy browsers thanks to <a rel="noreferrer" href="https://polyfill.io" target="_blank">polyfill services</a> and other
available options to support legacy browsers.</p>
<p><a class="btn btn-lg btn-default btn-empty" href="https://github.com/thednp/bootstrap.native/wiki/Browser-support" role="button">More</a></p>
</div>
</div>
</div>
<div class="item">
<div class="item-bg bg-purple"></div>
<div class="container">
<div class="carousel-caption">
<h2>What's the fuzz?</h2>
<p>If you use mainly Bootstrap plugins in your projects, you may drop jQuery right away. Why load jQuery if you don't really need it? It's too old school!</p>
<p><a class="btn btn-lg btn-default btn-empty" rel="noreferrer" href="http://jsperf.com/jquery-vs-native-element-performance" target="_blank" role="button">See Now</a></p>
</div>
</div>
</div>
<div class="item">
<div class="item-bg bg-red"></div>
<div class="container">
<div class="carousel-caption">
<h2>This was a <b>Carousel</b> example</h2>
<p>One component of this library that works just as the original jQuery plugin but with much less amount of code, higher code quality and with some smart optimizations.</p>
<p><a class="btn btn-lg btn-default btn-empty" href="#componentCarousel" role="button">How to use</a></p>
</div>
</div>
</div>
</div>
<a href="#myCarousel" class="left carousel-control" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
<a href="#myCarousel" class="right carousel-control" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div>
<div id="main-container" class="container">
<div class="row">
<div id="container-content-column" class="col-sm-9" data-spy="scroll" data-target="#side-nav" data-offset="80">
<section id="use">
<h2>Read First</h2>
<p>Welcome to the demo page! Before you do anything, please make sure to check the <a href="https://github.com/thednp/bootstrap.native/wiki">Wiki pages</a>, as they give you a quick start with using the library and also provide a good set of valuable guides on how to get the best out of it.</p>
<div class="list-group">
<a class="list-group-item" href="https://github.com/thednp/bootstrap.native/wiki/Acknowledgements">
<h4 class="list-group-item-heading">Acknowledgements</h4>
<p class="list-group-item-text">A quick note on some of the similarities and differences with the original jQuery plugins. Nothing to worry about, but still good to know all the tricks on how to maximize your workflow.</p>
</a>
<a class="list-group-item" href="https://github.com/thednp/bootstrap.native/wiki/How-to-use">
<h4 class="list-group-item-heading">How to use</h4>
<p class="list-group-item-text">An indepth guide on how to use it with stuff like <code>npm</code>, <code>RequireJS</code> or <b>CDN</b> links.</p>
</a>
<a class="list-group-item" href="https://github.com/thednp/bootstrap.native/wiki/FAQs">
<h4 class="list-group-item-heading">FAQs</h4>
<p class="list-group-item-text">A short list of frequent asked questions regarding the use of the library.</p>
</a>
<a class="list-group-item" href="https://github.com/thednp/bootstrap.native/wiki/Browser-support">
<h4 class="list-group-item-heading">Browser Support</h4>
<p class="list-group-item-text">A word on how to enable the library for legacy browsers.</p>
</a>
<a class="list-group-item" href="https://github.com/thednp/bootstrap.native/wiki/About">
<h4 class="list-group-item-heading">About</h4>
<p class="list-group-item-text">Learn about the <code>bootstrap.native</code> project inception, goals and motivations.</p>
</a>
</div>
<p>And now, let's get down to business!</p>
</section>
<section id="components">
<h2>Components</h2>
<section id="componentModal">
<h3>Modal</h3>
<p>The <b>Modal</b> component works exactly as the original in most regards, with minor exceptions on options, events and public methods. It provides both an initialization option and a public method to write directly into your modal content.</p>
<p>In addition to adapting the spacing (padding) of the <code><body></code> elements like <code><nav class="navbar-fixed-top"></code> are also adapted in the same manner to get the smoothest possible transition. Like all components of the library, the component has the ability to provide access to the initialization even if your modal is setup via DATA API.</p>
<section id="modalOptions">
<h4>Options</h4>
<p>The <code>remote</code> option have been removed in Bootstrap 4 and we already replaced it with a better option. We also skipped the <code>show</code> option because via JavaScript we quickly and easily access
the component's public methods right after initialiation, we'll have a look in a minute.</p>
<div class="table-responsive">
<table class="table table-bordered table-striped js-options-table">
<thead>
<tr>
<th>Name</th>
<th>type</th>
<th>default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>backdrop</code></td>
<td><em>boolean</em> or the <em>string</em> <code>'static'</code></td>
<td>true</td>
<td>Includes a modal-backdrop element. Alternatively, specify <code>'static'</code> for a backdrop which doesn't close the modal on click.</td>
</tr>
<tr>
<td><code>keyboard</code></td>
<td><em>boolean</em></td>
<td>true</td>
<td>Option to dismiss the current modal via <kbd>Esc</kbd> key.</td>
</tr>
<tr>
<td><code>content</code></td>
<td><em>markup</em></td>
<td></td>
<td>The <b>Modal</b> component comes with a template system instead of a load <b>remote</b> content function (Bootstrap 4 drops it). This option can be used with JavaScript only.</td>
</tr>
</tbody>
</table>
</div>
<p>The default options' values are same as their jQuery plugin equivalents so you can expect same behavior.</p>
</section>
<section id="modalMethods">
<h4>Methods</h4>
<p>For further control the <b>Modal</b> component exposes a couple of public methods to be used via JavaScript :</p>
<div class="list-group">
<div class="list-group-item">
<h4 class="list-group-item-heading">.show()</h4>
<p class="list-group-item-text">The method that shows an initialized modal. When called, it will also hide any other visible modal before showing the one requested, making sure to keep the backdrop in place.</p>
</div>
<div class="list-group-item">
<h4 class="list-group-item-heading">.hide()</h4>
<p class="list-group-item-text">This hides an initialized modal. Additionally it will also close (if enabled) the backdrop.</p>
</div>
<div class="list-group-item">
<h4 class="list-group-item-heading">.toggle()</h4>
<p class="list-group-item-text">When called it shows the modal if hidden and hides it otherwise, using the above two methods.</p>
</div>
<div class="list-group-item">
<h4 class="list-group-item-heading">.setContent()</h4>
<p class="list-group-item-text">The method to enable you to set/override the content of <code><div class="modal-content"></code> element of your modal at any time, but you might want to avoid
using this method while the modal is animating.</p>
</div>
<div class="list-group-item">
<h4 class="list-group-item-heading">.update()</h4>
<p class="list-group-item-text">This allows you to update the modal layout (handling overflowing/non-overflowing body and/or modal) after you have changed it's content or other layout changes occured.
This would naturally follow the previous <code>.setContent()</code> method.</p>
</div>
</div>
</section>
<section id="modalEvents">
<h4>Events</h4>
<div class="table-responsive">
<table class="table table-bordered table-striped bs-events-table">
<thead>
<tr>
<th>Event Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>show.bs.modal</code></td>
<td>This event fires immediately when the <code>.show()</code> instance method is called. If caused by a click and the clicked element is a modal triggering element, that element is available
as the <code>event.relatedTarget</code> property of the event.</td>
</tr>
<tr>
<td><code>shown.bs.modal</code></td>
<td>This event is fired when the modal has been made visible to the user. The <code>event.relatedTarget</code> is same as for the above.</td>
</tr>
<tr>
<td><code>hide.bs.modal</code></td>
<td>This event is fired immediately when the <code>.hide()</code> instance method has been called.</td>
</tr>
<tr>
<td><code>hidden.bs.modal</code></td>
<td>This event is fired when the modal has finished being hidden from the user.</td>
</tr>
</tbody>
</table>
</div>
<p>The <code>loaded.bs.modal</code> original event is not needed, also because we replaced the <code>remote</code> option with another one. If modal is opened via JavaScript methods, or by clicking
on another element that is not a modal triggering element, the <code>relatedTarget</code> is <em>null</em>.</p>
</section>
<section id="modalUsage">
<h4>Usage</h4>
<h5 id="modalDataAPI">Via DATA API</h5>
<p>You can initialize <b>Modal</b> without writing any code as long as you have a modal and a trigger with <code>data-target</code> or a link with <code>href</code> referencing that modal. The component
will initialize for all elements with <code>data-toggle="modal"</code> found in the DOM.</p>
<pre><code class="language-markup"><!-- provide a trigger button -->
<button id="myModalTrigger" type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
<!-- Alternatively provide a link -->
<a id="myModalTrigger" data-toggle="modal" href="#myModal">Launch modal</a>
<!-- also the modal itself -->
<div id="myModal" class="modal fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
Some content
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</code></pre>
<p>The DATA API is suited for static content dialogs.</p>
<h5>Via JavaScript</h5>
<p>When you write your code, first make sure a modal template is present in the DOM. Generally you can initialize <b>Modal</b> for any instance of <code><div class="modal"></code> and immediately get access to methods. Alternativelly you can initialize on a triggering element, similar to how the DATA API works, in this case the <code>data-toggle="modal"</code> is not required, but you need to specify a modal reference via a specific attribute, like so <code><button data-target="#modalId"></code> or <code><a href="#modalId"></code>.</p>
<p>The JavaScript way is the <b>only</b> way to deal with dynamically added modals. Let's create a very basic modal template for the record.</p>
<pre><code class="language-markup"><!-- blank modal template -->
<div id="modalID" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- the .setContent() method will update this element's HTML -->
</div>
</div>
</div>
</code></pre>
<p>If you are looking for the functionality that grants you full control over your modal without the need of a triggering element, you can initialize that modal and get access to public methods right away:</p>
<pre><code class="language-javascript">// get the modal by ID
var myModal = document.getElementById('modalID');
// initialize on a <div class="modal"> with all options
// Note: options object is optional
var myModalInstance = new Modal(myModal,
{ // options object
content: '<div class="modal-body">Some content to be set on init</div>', // sets modal content
backdrop: 'static', // we don't want to dismiss Modal when Modal or backdrop is the click event target
keyboard: false // we don't want to dismiss Modal on pressing Esc key
});
// OR initialize and show the modal right away
var myModalInstance = new Modal(myModal, options);
myModalInstance.show();
// now you know why we don't need a show option
</code></pre>
<p>In other cases you can designate one or multiple triggering elements to open your modal, they will be the target of the constructor initialization and allow you to use same modal as a template to handle contents pushed by the triggering elements.</p>
<pre><code class="language-markup"><!-- <button> referencing the modal -->
<button type="button" id="buttonID" class="btn btn-primary" data-target="#modalID">My Button</button>
<!-- <a> referencing the modal -->
<a id="anchorID" class="btn btn-primary" href="#modalID">My Link</a>
</code></pre>
<p>Now let's initialize one of them right away:</p>
<pre><code class="language-javascript">// get a certain button trigger
var myButton = document.getElementById('buttonID');
// initiate Modal on a triggering element
var myModalInstance = new Modal(myButton,
{ // options object
content: '<div class="modal-body">Some content to be set on init</div>', // sets modal content
keyboard: false // we don't want to dismiss Modal on pressing Esc key
});
</code></pre>
<p>Now we have an initialization reference in <code>myModalInstance</code>, regardless of which element is the target of our constructor, we can start applying the component's public methods.</p>
<pre><code class="language-javascript">// show the modal at any time
myModalInstance.show();
// hide the modal
myModalInstance.hide();
// toggle the modal (show/hide)
myModalInstance.toggle();
// change the modal content
myModalInstance.setContent('<div class="modal-body">Some different content</div>');
// if the above method is used while modal was shown, you can then ask for a layout update
myModalInstance.update();
</code></pre>
<p>After initialization via DATA API or JavaScript, we also have access to the component's original events.</p>
<pre><code class="language-javascript">// GET THE EVENT TARGET, THE MODAL
// when we are certain which modal ID to work with
var myModal = document.getElementById('modalID');
// also button trigger related (especially when modals are targeted by multiple triggering elements)
// a triggering element is a link
var myModal = document.getElementById(myModalTriggerButton.getAttribute('href').replace('#',''));
// OR triggering element is not a link
var myModal = document.getElementById(myModalTriggerButton.getAttribute('data-target').replace('#',''));
// ATTACH HANDLERS
// show.bs.modal event
myModal.addEventListener('show.bs.modal', function(event){
// do something when this event triggers
// event.target is the modal referenced in myModal
// event.relatedTarget is the button referenced with myModalTriggerButton
}, false);
// shown.bs.modal event
myModal.addEventListener('shown.bs.modal', function(event){
// do something when this event triggers
// event.target is the modal referenced in myModal
// event.relatedTarget is the button referenced with myModalTriggerButton
}, false);
// hide.bs.modal event
myModal.addEventListener('hide.bs.modal', function(event){
// do something when this event triggers
// event.target is the modal referenced in myModal
}, false);
// hidden.bs.modal event
myModal.addEventListener('hidden.bs.modal', function(event){
// do something when this event triggers
// event.target is the modal referenced in myModal
}, false);
</code></pre>
<p>Additionally the component will store in the modal OR its triggering elements some references, for the internal execution, hopefully would help you as well. For instance, when a modal is referenced by one or more triggering elements, it will know which one was clicked last time to execute the <code>.show()</code> instance method.</p>
<pre><code class="language-javascript">// for modals with multiple triggering elements, the value changes every time the triggering element was clicked
var lastModalTrigger = myModal.modalTrigger;
// for modals targeted directly by the component with no triggering element
// you can access the initialization object from the modal itself
var myModalInit = myModal.Modal;
</code></pre>
<p>Also each triggering element holds the initialization of the <b>Modal</b> component:</p>
<pre><code class="language-javascript">// when a modal was initialized via DATA API
// OR a triggering element is the target of the Modal constructor
var myModalInstance === document.getElementById('myModalTrigger').Modal;
</code></pre>
<p>These references are used internally to hide currently visible modals when showing another one. When the component is used via JavaScript only without a triggering element, you need to manually hide (via <code>.hide()</code> instance method) any visible modal before showing another modal (via the <code>.show()</code> instance method).</p>
</section>
<section id="modalExamples">
<h4>Examples</h4>
<h5>Using the DATA API</h5>
<p>The first example is a modal with static content initialized via DATA API, exactly as described in the above <a href="#modalDataAPI">Use via DATA API</a> section, and showcasing the
ability to show another modal from a modal currently visible.</p>
<p><button type="button" class="btn btn-warning btn-lg" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-heart"></i> Launch simple modal</button></p>
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<h4>Text in a modal</h4>
<p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula.</p>
<h4>Popover in a modal</h4>
<p>This <a href="#" style="margin:-5px 0 0 0" role="button" data-toggle="popover" class="btn btn-default popover-test" data-content="And here's some amazing content. It's very engaging. right?" data-title="A Title">button</a> should trigger a popover on click.</p>
<h4>Tooltips in a modal</h4>
<p><a href="#" class="tooltip-test" data-toggle="tooltip" title="Tooltip">This link</a> and <a href="#" data-toggle="tooltip" class="tooltip-test" title="Tooltip">that link</a> should have tooltips on hover.</p>
<h4>Open modal from modal</h4>
<p>Click <a data-toggle="modal" href="#anotherModal">here</a> to open another modal.</p>
<hr>
<h4>Overflowing text to show scroll behavior</h4>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#anotherModal">Next</button>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
</div>
</div>
</div>
</div>
<div id="anotherModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="anotherModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="anotherModalLabel">Another Modal Title</h4>
</div>
<div class="modal-body">
<h4>This modal was opened from another modal</h4>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
</div>
<div class="modal-footer">
<button data-toggle="modal" data-target="#myModal" type="button" class="btn btn-primary">Back</button>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
</div>
</div>
</div>
</div>
<h5>Via JavaScript</h5>
<p>The following examples are focused on everything the <b>Modal</b> component offers for JavaScript initialization and usage. Given a modal template and some buttons to open the modal on click,
let's initialize it first:</p>
<pre><code class="language-javascript">// we grab a modal by ID
var myModal = document.getElementById('myModal');
// we grab some button by ID, we will use it later
var btnModal = document.getElementById('openModalViaJS');
// this button IS NOT a triggering element, as it has no reference to the above modal
// set a custom content
var firstModalContent = '<div class="modal-header">'
+'<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
+'<span aria-hidden="true">×</span>'
+'</button>'
+'<h4 class="modal-title" id="myModalJSLabel">Modal title</h4>'
+'</div>'
+'<div class="modal-body">'
+'<p>This is where you fill up content you know, etc.</p>'
+'</div>'
+'<div class="modal-footer">'
+'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'
+'</div>';
// initialize Modal for this triggering element
var modalInitJS = new Modal(myModal, {
content: firstModalContent,
backdrop: 'static'
});
// OR initialize with no options provided
// the options object is optional
var modalInitJS = new Modal(myModal);
// when we click btnModal, open the modal
btnModal.addEventListener('click', function(e){
modalInitJS.show();
}, false)
// BONUS
// since there is no triggering element, you might need
// access to the initialization object from another application
var findModalInitJS = myModal.Modal;
</code></pre>
<p>Here is what the above code does:</p>
<p><button id="openModalViaJS" type="button" class="btn btn-default btn-lg">Open Modal</button></p>
<p>Next we will initialize another triggering button, and attach a handler to it to change content of the modal when clicked.</p>
<pre><code class="language-javascript">// the triggering element
var btnModal2 = document.getElementById('openModalViaJS2');
// set some custom content or get if from external sources
var externalModalContent = {
title: 'A modal title',
content: 'Modal content to fill the modal-body.',
};
// set a custom modal-content template
var secondModalContent =
'<div class="modal-header">'
+'<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
+'<span aria-hidden="true">×</span>'
+'</button>'
+'<h4 class="modal-title" id="gridModalLabel">' + externalModalContent.title + '</h4>'
+'</div>'
+'<div class="modal-body">'
+'<p>' + externalModalContent.content + '</p>'
+'</div>'
+'<div class="modal-footer">'
+'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'
+'</div>';
// initialize Modal for this triggering element
var modalInitJS2 = new Modal(btnModal2, { backdrop: 'static' });
// because we initialized the component for this button
// this becomes the triggering element to hold the initialization object
var modalInitJSInit2 = btnModal2.Modal;
// now when we click this modal triggering element, we change the modal content
btnModal2.addEventListener('click', function() {
modalInitJS2.setContent(secondModalContent);
}, false);
</code></pre>
<p>By changing the <code>innerHTML</code> of the <code>modal-header</code>, <code>modal-body</code> or <code>modal-footer</code> with variables, you can achieve exactly the same as the
<a href="http://getbootstrap.com/javascript/#modals-related-target" rel="noreferrer" target="_blank">other examples</a> from the demo of the original plugin. So we use same modal, but with different content:</p>
<p><a id="openModalViaJS2" class="btn btn-default btn-lg">Open Modal</a></p>
<p>Now if you go back to the previous triggering button and click it, you will notice that the modal is still like it was set by the second triggering button. Let's reset the modal content:</p>
<pre><code class="language-javascript">// we grab the button by ID
var btnModalNotTrigger = document.getElementById('modalNotTriggerJS');
// simply attach a click handler to it
btnModalNotTrigger.addEventListener('click', function() {
modalInitJS.setContent(firstModalContent); // revert modal content back to previous
modalInitJS.show(); // also show the modal
}, false);
</code></pre>
<p>Back to the previous modal content:</p>
<p><a id="modalNotTriggerJS" class="btn btn-default btn-lg" href="#">Reset</a></p>
<div id="myModalJS" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalJSLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<p>Another example is using the <code>.update()</code> method. Let's say we have a modal initialized via DATA API, you might think we cannot access this instance's methods. Remember the trigger button
stores the initialization?</p>
<pre><code class="language-javascript">var modalUpdate = document.getElementById('modalUpdate'), // the trigger
anotherStaticModal = document.getElementById('anotherStaticModal'), // the modal
currentStaticModalBody = anotherStaticModal.querySelector('.modal-body'), // the body of the current modal
currentStaticModalBodyContent = currentStaticModalBody.innerHTML, // we cache the content of the body
modalUpdateInit = modalUpdate.Modal, // the initialization
changeModal1 = document.getElementById('changeModal1'), // the change buttons
changeModal2 = document.getElementById('changeModal2');
changeModal1.addEventListener('click', function(){
currentStaticModalBody.innerHTML = currentStaticModalBodyContent;
modalUpdateInit.update();
}, false);
changeModal2.addEventListener('click', function(){
currentStaticModalBody.innerHTML = '<h4>This modal changed via JavaScript</h4>';
currentStaticModalBody.innerHTML += '<p>Something you would want to be displayed in the body.</p>';
modalUpdateInit.update();
}, false);
</code></pre>
<p>A quick demo to showcase the above script:</p>
<p><a id="modalUpdate" class="btn btn-default btn-lg" data-toggle="modal" href="#anotherStaticModal">Show modal</a></p>
<div id="anotherStaticModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="anotherStaticModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="anotherStaticModalLabel">Some Modal Title</h4>
</div>
<div class="modal-body">
<h4>This modal is initialized via DATA API</h4>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p class="text-info lead">Scroll down.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p class="text-info lead">Scroll some more.</p>
<p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<p>Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Donec sed odio dui. Donec ullamcorper nulla non metus auctor fringilla.</p>
</div>
<div class="modal-footer">
<button id="changeModal1" type="button" class="btn btn-primary hidden">Set content 1</button>
<button id="changeModal2" type="button" class="btn btn-primary">Set content 2</button>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
</div>
</div>
</div>
</div>
</section>
</section>
<section id="componentDropdown">
<h3>Dropdown</h3>
<p>The <b>Dropdown</b> component works like the original jQuery plugin and offers an additional option and an ability to handle click event in a way that you can create nested dropdowns and other cool stuff with ease.</p>
<p>Starting with version 2.0.20 the component supports some more robust accessibility features:</p>
<ul>
<li><kbd>Up</kbd> and <kbd>Down</kbd> arrow keys will users to select menu items, and also when pressed, the component will prevent default scroll behavior;</li>
<li><kbd>Esc</kbd> key will now close the dropdown, a feature previously supported, but reworked it's <code>keyHandler</code> to support the above new features, so it will close the last open nested dropdown element;</li>
<li><kbd>Enter</kbd> key will work as if click event triggered, the new event handler expectes the default browser behavior, let's hope it stays that way;</li>
<li>opening/closing the dropdown now it will focus the triggering button (on close) or the first focusable element from inside the dropdown menu (on open).</li>
</ul>
<section id="dropdownOptions">
<h4>Options</h4>
<div class="table-responsive">
<table class="table table-bordered table-striped js-options-table">
<thead>
<tr>
<th>Name</th>
<th>type</th>
<th>default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>persist</code></td>
<td><em>boolean</em></td>
<td>false</td>
<td>Option to keep the dropdown-menu open when <code>click</code> event targets a child element, useful for forms or interactive content. Use <code>data-persist="true"</code> attribute to enable this functionality via DATA-API.</td>
</tr>
</tbody>
</table>
</div>
</section>
<section id="dropdownMethods">
<h4>Methods</h4>
<div class="list-group">
<div class="list-group-item">
<h4 class="list-group-item-heading">.toggle()</h4>
<p class="list-group-item-text">For a given initialization the method shows the dropdown-menu if hidden or hides it otherwise.</p>
</div>
</div>
</section>
<section id="dropdownEvents">
<h4>Events</h4>
<p>Just like the original jQuery plugin, the <b>Dropdown</b> component covers this part as well:</p>
<div class="table-responsive">
<table class="table table-bordered table-striped bs-events-table">
<thead>
<tr>
<th>Event Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>show.bs.dropdown</code></td>
<td>This event fires immediately when the show instance method is called.</td>
</tr>
<tr>
<td><code>shown.bs.dropdown</code></td>
<td>This event is fired when the dropdown has been made visible to the user.</td>
</tr>
<tr>
<td><code>hide.bs.dropdown</code></td>
<td>This event is fired immediately when the hide instance method has been called.</td>
</tr>
<tr>
<td><code>hidden.bs.dropdown</code></td>
<td>This event is fired when the dropdown has finished being hidden from the user.</td>
</tr>
</tbody>
</table>
</div>
<p>The target of all component events is the parent <code><div class="dropdown"></code>, while the <code>event.relatedTarget</code> is the triggering element with the specific
<code>data-toggle="dropdown"</code> attribute. If you dismiss the dropdown via JavaScript, the <kbd>Esc</kbd> key or by clicking outside the triggering button, the <code>event.relatedTarget</code>
is <em>null</em>.</p>
</section>
<section id="dropdownUsage">
<h4>Usage</h4>
<h5>Via DATA API</h5>
<p>Notice we use same markup as the original example, the triggering element with the <code>data-toggle="dropdown"</code> attribute will initialize the <b>Dropdown</b> component.</p>
<pre><code class="language-markup"><!-- basic dropdown template -->
<div class="dropdown">
<button id="myDropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown trigger
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="myDropdown">
...
</ul>
</div>
</code></pre>
<h5>Via JavaScript</h5>
<p>When you want full control or you need a way to handle dynamically generated dropdown-menus, you can do it via JavaScript. Considering the above HTML was inserted into the DOM, let's initialize it real quick:</p>
<pre><code class="language-javascript">// get some element that has the data-toggle="dropdown"
var myDropdown = document.getElementById('myDropdown');
// initialize component with it's 'persist' option
var myDropdownInit = new Dropdown( myDropdown, true );
// OR initialize component with no option
// the option is optional
var myDropdownInit = new Dropdown( myDropdown );
</code></pre>
<p>All right, now we have an initialization reference, we can get to work with the component's method:</p>
<pre><code class="language-javascript">// assuming the above, we can
myDropdownInit.toggle();
// or we can simply toggle the dropdown on initialization
var newDropdownInitialization = new Dropdown(myDropdown).toggle();
</code></pre>
<p>Also assuming the above HTML was used for your dropdown-menu, we can also attach the original events to the parent of the triggering element, which is the <code><div class="dropdown"></code> element:</p>
<pre><code class="language-javascript">// show.bs.dropdown
myDropdown.parentNode.addEventListener('show.bs.dropdown', function(event){
// do something when the event fires
// event.target is the PARENT of the triggering element
// event.relatedTarget is the triggering element
}, false);
// shown.bs.dropdown
myDropdown.parentNode.addEventListener('shown.bs.dropdown', function(event){
// do something when the event fires
// event.target is the PARENT of the triggering element
// event.relatedTarget is the triggering element
}, false);
// hide.bs.dropdown
myDropdown.parentNode.addEventListener('hide.bs.dropdown', function(event){
// do something when the event fires
// event.target is the PARENT of the triggering element
}, false);
// hidden.bs.dropdown
myDropdown.parentNode.addEventListener('hide.bs.dropdown', function(event){
// do something when the event fires
// event.target is the PARENT of the triggering element
}, false);
</code></pre>
<p>When your dropdown-menus have a different structure and the <code>myDropdown.parentNode</code> is NOT the <code><div class="dropdown"></code> element, you might consider a
<code>getClosest()</code> function to find it so you can then attach the original events to it.</p>
<p>Now, the <b>Dropdown</b> component also stores the initialization object in the targeted elements, just as for the modal triggering buttons:</p>
<pre><code class="language-javascript">// get some element we know it was initialized
var myDropdown = document.getElementById('myDropdown');
// reference the initialization
var myDropdownInit = myDropdown.Dropdown;
// use the public method
myDropdownInit.toggle();
</code></pre>
<p>Also you can do a simple check for the existence of <code>'Dropdown' in myDropdown</code> element to make sure you don't go the wrong way.</p>
</section>
<section id="dropdownExamples">
<h4>Examples</h4>
<p>Right out of the box, the above dropdown template is initialized via DATA-API right away.</p>
<div class="btn-group" style="margin-bottom:10px">
<div class="dropdown">
<button class="btn btn-primary" id="myDropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown trigger <span class="caret"></span></button>
<ul class="dropdown-menu" aria-labelledby="myDropdown">
<li role="presentation"><a role="menuitem" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" href="#">Another action</a></li>
<li role="presentation"><a role="menuitem" href="#">Something else here</a></li>
</ul>
</div>
</div>
<p>Ok now let's insert a new dropdown-menu and initialize right after via JavaScript.</p>
<pre><code class="language-javascript">// let's do it via a click handler to the following element
var makeMeDropdown = document.getElementById('makeMeDropdown');
// let's say we have a dropdown template as the above
// you may also reference one that you created yourself
var myDropdownTemplate = '<div class="dropdown">a valid template</div>';
makeMeDropdown.addEventListener('click', function(e){
e.preventDefault();
// don't do this again
if ( !/\bdisabled/.test(makeMeDropdown.className) ){
// invalidate the makeMeDropdown
this.setAttribute('disabled',true);
this.className = 'btn btn-default disabled';
this.innerHTML = 'All done';
// we inject the dropdown
this.parentNode.innerHTML += myDropdownTemplate;
// or use appendChild when myDropdownTemplate is an Element instance
// this.parentNode.appendChild( myDropdownTemplate );
// get a reference to the new dropdown
var formDropdown = document.getElementById('formDropdown');
// initiate with option
var DropdownInit = new Dropdown(formDropdown, true);
// also attach Dropdown original events
formDropdown.parentNode.addEventListener('show.bs.dropdown', function(e){
console.log('Do something when the event fires');
}, false);
formDropdown.parentNode.addEventListener('shown.bs.dropdown', function(e){
console.log('Do something when the event fires');
}, false);
formDropdown.parentNode.addEventListener('hide.bs.dropdown', function(e){
console.log('Do something when the event fires');
}, false);
formDropdown.parentNode.addEventListener('hidden.bs.dropdown', function(e){
console.log('Do something when the event fires');
}, false);
}
}, false);
</code></pre>
<p>That's all there is to do with <b>Dropdown</b> initialization, let's check a quick demo.</p>
<div class="btn-group" style="margin-bottom:10px">
<button id="makeMeDropdown" class="btn btn-success">Make me a Dropdown</button>
</div>
<p>A quick last example is with nested dropdown-menus, as the component will look for the child elements and will prevent itself from closing if the click target is a child item with <code>data-toggle</code> attribute.</p>
<ul class="nav nav-pills">
<li class="dropdown">
<a id="drop1" href="#" class="dropdown-toggle" data-toggle="dropdown" data-trigger="mouseover" aria-haspopup="true" role="button" aria-expanded="false">Pill <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="drop1">
<li role="presentation"><a role="menuitem" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" href="#">Another action</a></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">This is nesting <span class="caret"></span></a>
<ul class="dropdown-menu right" role="menu" aria-labelledby="drop2">
<li role="presentation"><a role="menuitem" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" href="#">Another action</a></li>
<li role="presentation"><a role="menuitem" href="#">Something else here</a></li>
<li role="presentation" class="divider"></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Sub-menu <span class="caret"></span></a>
<ul class="dropdown-menu right" role="menu" aria-labelledby="drop3">
<li role="presentation"><a role="menuitem" href="#">Level 1</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Other stuff <span class="caret"></span></a>
<ul class="dropdown-menu right" role="menu" aria-labelledby="drop4">
<li role="presentation"><a role="menuitem" href="#">Level 2</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Even more stuff <span class="caret"></span></a>
<ul class="dropdown-menu right" role="menu" aria-labelledby="drop5">
<li role="presentation"><a role="menuitem" href="#">Level 3</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li role="presentation" class="divider"></li>
<li role="presentation"><a role="menuitem" href="http://google.cn" rel="noreferrer" target="_blank">External link</a></li>
</ul>
</li>
<li class="dropdown pull-right">
<a id="drop2" href="#" class="dropdown-toggle" data-toggle="dropdown" data-trigger="mouseover" aria-haspopup="true" role="button" aria-expanded="false">Nested <span class="caret"></span></a>
<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="drop2">
<li role="presentation"><a role="menuitem" href="#">Action</a></li>
<li role="presentation"><a role="menuitem" href="#">Another action</a></li>
<li role="presentation"><a role="menuitem" href="#">Something else here</a></li>
<li role="presentation" class="divider"></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Sub-menu <span class="caret"></span></a>
<ul class="dropdown-menu left" role="menu" aria-labelledby="drop3">
<li role="presentation"><a role="menuitem" href="#">Level 1</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Other stuff <span class="caret"></span></a>
<ul class="dropdown-menu left" role="menu" aria-labelledby="drop4">
<li role="presentation"><a role="menuitem" href="#">Level 2</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
<li role="presentation" class="dropdown">
<a role="menuitem" href="#" data-toggle="dropdown" aria-expanded="false">Even more stuff <span class="caret"></span></a>
<ul class="dropdown-menu left" role="menu" aria-labelledby="drop4">
<li role="presentation"><a role="menuitem" href="#">Level 3</a></li>
<li role="presentation"><a role="menuitem" href="#">Nested Menu</a></li>
</ul>
</li>
</ul>
</li>
</ul>