-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2650 lines (2538 loc) · 106 KB
/
index.html
File metadata and controls
2650 lines (2538 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~ JS-DOM/index.html of bbauska/dom for dom.bauska.org ~~~~~~~~~~~~~~~~~~~~-->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="title' content="DOM (Document Object Model) Utilization">
<meta name="keywords" content="steak,Steak,YouTube,Sonic,sonic,SONIC,Silus,Gaming,World,
Silus Gaming World,Luccas Neto,T-Series,SET India,Cocomelon,Mr Beast,MrBeast,Beast,CE1,C-E-1,CE-1,
MRBEAST,BEAST,Silus Gaming World,Silus,Silus Spider,Nick AR,Nick,NY,NYNick,New York Nick,
NY Nick,UR Cristiano,UR,Cristiano,Kids Dima Show,Vlad and Niki,Luisito Comunica,Bright Side,
Zee TV,Dude Perfect,Tips Official,Tips Official, FNAF,Five Nights at Freddies,The Music Company,
PewDiePie,5 minute crafts,A4,Sambucha,Sam & Colby,Sam&ColbyReacts,Juice World,Ray William
Johnson,FGTV,KaicenatLive,IShowSpeed,Jelly,DanTDM,KSI,Logan Paul,JackSucksAtLife,
PackGOD,JakePaul,Jake,Paul Jake Paul,SamGaming01,Veshreny,WWE,WorldWrestlingEntertainment,
Smosh,DadsSpeed,TopperGuild,InternetAnarchist,GuavaJuice,Fortnite Youtubers,Sypherpk,LiveSPEEDY,
AdenRoss,Preston,JohnnieJuilbert,YouTube Movies,AnnoyingOrange,JackSepticeye,JackFilms,BBNo$,
Unspeakable,MarkRober,TheFilmTheorist,TheGameTheorist,Minecraft,TheBoys,JTMusic,DAFUQBoom!?,
AIMIN,Robby,BrentTV,EYStream,Fortnite,Ninja,SunnyV2,N30N,SamFrank,Dream,Technoblade,
RyanTrhan,Stellar,DJCook,AnthonyMadia,AnthonyIlla,SmoshGaming,SmoshPit,SmoshCast,Fred,cellardoor,
Brian's Blog,Cellar Door,SML Theorist,Theorist,Silus Spider,#SilusGW,#SilusGamingWorld,#Silus-GW,
#Silus-Gaming-World,silas,#silas,#silasgw,silusgw,#silasgamingworld,silasgamingworld,#silusgwfnaf,
#silus-gw,#silusthesnake,#silusalienufo,#supersilus,#evilsilus,#youtubesilus,#silas,silas,puppetsilus,
robotsilus,aisilus,silus-et,#silus-et,dotexesilus,#dotexesilus,silusgwminecraft,#silusgamingworldminecraft,
silusgamingworldfnaf,#silusgamingworldfnaf,silusgametrailers,dotcomsilus,puppetsilus,robloxsilus,
robloxsilusgamingworld,silusgamingworldfortnite,silusgamingworldleagueoflegends,silusgamingworldfnafvhs,
silusgwfnafvhs,supersilus,silussquadmembers,silus-squad-members,silusthesnake,silusgwespanol,
silusspiderespanol,RyanTrhan,Stellar,DJCook,AnthonyMadia,AnthonyIlla,SmoshGaming,SmoshPit,SmoshCast,
Fred,cellardoor,BriansBlog,CellarDoor,Silus,GW,Gaming,World,SilusGamingWorld,SilusGW,#Silus-GW,#cellardoor,
bbauska,#SilusGamingWorld,#SilusGW,gluttony,selfishness,#bbauska,#silas,#silys,#sylus,#sylas,#sylusgw,
#silasGW,#silasgamingworld,#roblox,roblox,LearnJSwithBrian">
<meta name="description" content="How to utilize and maximize usage of the DOM - Document Object Model.">
<meta name="author" content="Brian Bauska">
<link rel="icon" type="image/x-icon" href="./images/favicon/favicon.ico">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>DOM (Document Object Model) Utilization</title>
<link href="/css/styles.css" rel="stylesheet" media="screen">
<link href='https://fonts.googleapis.com/css?family=Armata' rel='stylesheet' type='text/css'>
<!-- favicons -->
<link rel="apple-touch-icon" sizes="57x57" href="./images/favicon/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="./images/favicon/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="./images/favicon/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="./images/favicon/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="./images/favicon/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="./images/favicon/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="./images/favicon/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="./images/favicon/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="./images/favicon/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="./images/favicon/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="./images/favicon/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="./images/favicon/favicon-16x16.png">
<link rel="manifest" href="./manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="./images/favicon/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~ script function: scrollFunction() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<script>
<!-- get the button -->
var mybutton = document.getElementById("myBtn");
<!-- when the user scrolls down 10px from the top of the document, show the button -->
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 10 || document.documentElement.scrollTop > 10) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
<!-- when the user clicks on the button, scroll to the table of contents. -->
// when the user clicks on the button, scroll to the table of contents near top of document.
function topFunction() {
document.getElementById('toc').scrollIntoView();
}
</script>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h1 id="toc">Table of Contents - DOM (Document Object Model) Utilization</h1>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ol>
<li><a href="#D01">JavaScript - How to Manipulate DOM Elements?</a></li>
<li><a href="#D02">How to Add a <b><mark>Class</mark></b> to <b><mark>DOM Element</mark></b> in
JavaScript?</a></li>
<li><a href="#D03">How to select DOM Elements in JavaScript?</a></li>
<li><a href="#D04">How to get all ID's of the DOM elements with JavaScript?</a></li>
<li><a href="#D05">JavaScript – How to Get the Data Attributes of an Element?</a></li>
<li><a href="#D06">How To Get Element By Class Name In JavaScript?</a></li>
<li><a href="#D07">How to Get Value by Class Name using JavaScript?</a></li>
<li><a href="#D08">How to Get Domain Name From URL in JavaScript?</a></li>
<li><a href="#D09">How to Get Protocol, Domain and Port from URL using JavaScript?</a></li>
<li><a href="#D10">How to Extract the Host Name from URL using JavaScript?</a></li>
<li><a href="#D11">How to Get the Current URL using JavaScript?</a></li>
<li><a href="#D12">How to Get URL Parameters using JavaScript?</a></li>
<li><a href="#D13">How to Parse URL using JavaScript?</a></li>
<li><a href="#D14">Manipulating HTML Elements with JavaScript</a></li>
<li><a href="#D15">How to use <b><mark>innerHTML</mark></b> in JavaScript?</a></li>
<li><a href="#D16">JavaScript <b><mark>innerHTML</mark></b></a></li>
</ol>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h1 align="center">DOM</h1>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h2 id="D01">01 JavaScript - How to Manipulate DOM Elements?</h2>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>The <b><mark>DOM</mark></b> stands for the <b><mark>Document Object Model (DOM)</mark></b>,
which allows us to interact with the document and change its structure, style, and content. We can
use the <b><mark>DOM</mark></b> to change the content and style of an HTML element by changing its
properties.</p>
<p>We can manipulate or change the <b><mark>DOM elements</mark></b> by using the following methods:</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>1. Change the Content of an Element</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>You can change the content inside an HTML element using JavaScript. The two most common properties
for this are <b><mark>innerHTML</mark></b> and <b><mark>textContent:</mark></b></p>
<ul>
<li><b><mark>innerHTML</mark></b>: Allows you to get or set the HTML content inside an element.</li>
<li><b><mark>textContent</mark></b>: Allows you to get or set the text content inside an element,
ignoring any HTML tags.</li>
</ul>
<pre>
<body>
<div id="example1">This is the original content using <b><mark>innerHTML</mark></b>.</div>
<div id="example2">This is the original text content using <b><mark>textContent</mark></b>.</div>
<button onclick="changeContent()">Change Content</button>
<script>
// Function to change content
function changeContent() {
document.getElementById("example1").innerHTML =
"<strong>This is changed using innerHTML!</strong>";
document.getElementById("example2").textContent =
"This is changed using textContent!";
}
</script>
</body>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~ 01. change text and make bold using innerHTML ~~~~~~~~~~~~~~~~~~~~~~-->
<a href="https://codepen.io/editor/BBauska/pen/019e29fd-1fb3-7ccc-ac0a-474f3e6c17dd"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image001.gif?raw=true"
loading="lazy"
style="width:531px; height:94px;"
aria-label="Change text, make bold using innerHTML"
title="Change text, make bold using innerHTML"
alt="Change text, make bold using innerHTML." />
</a>
<!-- image001.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>In this example</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ul>
<li><b><mark>innerHTML</mark></b> changes the entire content of an element, including HTML tags.
In this case, we replace the content of the first <b><mark>div</mark></b> with bold text
using <b><mark><strong></mark></b>.</li>
<li><b><mark>textContent</mark></b> changes only the text inside the element, ignoring any HTML
tags. The second <b><mark>div</mark></b> is updated with plain text, without any HTML formatting.</li>
<li>The first <b><mark>div</mark></b> shows "This is the original content using <b><mark>innerHTML</mark></b>."</li>
<li>The second <b><mark>div</mark></b> shows "This is the original text content using <b><mark>textContent</mark></b>."</li>
<li>After clicking the "Change Content" button.</li>
<li>The first <b><mark>div</mark></b> will display "This is changed using <b><mark>innerHTML</mark></b>!"
with bold text.</li>
<li>The second <b><mark>div</mark></b> will display "This is changed using <b><mark>textContent</mark></b>!"
with plain text.</li>
</ul>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>2. Manipulate the Class Attribute</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>You can add, remove, or toggle <b><mark>classes</mark></b> on an element using JavaScript. This
is helpful for styling or applying animations.</p>
<ul>
<li><b><mark>classList.add()</mark></b>: Adds a <b><mark>class</mark></b> to an element.</li>
<li><b><mark>classList.remove()</mark></b>: Removes a <b><mark>class</mark></b> from an element.</li>
<li><b><mark>classList.toggle()</mark></b>: Toggles a <b><mark>class</mark></b>
(adds it if it's not present, removes it if it is).</li>
</ul>
<pre>
<html>
<head>
<style>
.highlight {
color: red;
font-weight: bold;
}
.bold {
font-weight: bold;
}
</style>
</head>
<body>
<div id="example" class="bold">This is a text element with the "bold" class.</div>
<button onclick="addClass()">Add 'highlight' Class</button>
<button onclick="removeClass()">Remove 'bold' Class</button>
<button onclick="toggleClass()">Toggle 'highlight' Class</button>
<script>
function addClass() {
document.getElementById("example").classList.add("highlight");
}
function removeClass() {
document.getElementById("example").classList.remove("bold");
}
function toggleClass() {
document.getElementById("example").classList.toggle("highlight");
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~ 02. highlight, remove, toggle text in class ~~~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e29ff-7d30-72c2-b968-91e49dcb8d47"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image002.gif?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Add highlight class, remove bold class, or toggle highlight class"
title="Add highlight class, remove bold class, or toggle highlight class"
alt="Add highlight class, remove bold class, or toggle highlight class." />
</a><!-- image002.gif -->
</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>In this example</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ul>
<li>Adding a Class (<b><mark>addClass()</mark></b>): When you click the "Add 'highlight' Class"
button, the highlight class is added to the div element with the id="example". This changes
the text color to red and makes it bold (as defined in the CSS).</li>
<li>Removing a Class (<b><mark>removeClass()</mark></b>): When you click the "Remove 'bold' Class"
button, the bold class is removed from the div, which removes the bold styling from the text.</li>
<li>Toggling a Class (<b><mark>toggleClass()</mark></b>): When you click the "Toggle 'highlight'
Class" button, the highlight class is either added or removed, depending on whether it's
already present. If the class is present, it will be removed; if not, it will be added.</li>
</ul>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>3. Set CSS Styles Using JavaScript</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>You can directly manipulate the CSS styles of an element using the style property. This allows you
to dynamically change how elements appear on the page.</p>
<pre>
// Changing multiple CSS properties
document.getElementById("demo").style.color = "red";
document.getElementById("demo").style.fontSize = "20px";
// Adding more than one style
document.getElementById("demo").style.cssText = "color: blue; font-size: 18px;";
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>4. Create, Add, and Remove Elements</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Sometimes, you need to create new elements, add them to the DOM, or remove existing ones. You
can do this easily with the following methods;</p>
<ul>
<li><b><mark>document.createElement()</mark></b>: Creates a new element.</li>
<li><b><mark>appendChild()</mark></b>: Adds a new element to a parent element.</li>
<li><b><mark>removeChild()</mark></b>: Removes a child element from a parent.</li>
</ul>
<pre>
// Create a new element
let newDiv = document.createElement("div");
newDiv.textContent = "This is a new div";
// Add the new element to the DOM
document.body.appendChild(newDiv);
// Remove an element from the DOM
document.body.removeChild(newDiv);
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>5. Insert Elements at a Specific Position</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>You can insert new elements at specific positions relative to existing elements using methods
like <b><mark>insertBefore()</mark></b>.</p>
<pre>
// Create a new element
let newDiv = document.createElement("div");
newDiv.textContent = "This is a new div";
// Find an existing element to insert before
let referenceNode = document.getElementById("referenceElement");
// Insert the new element before the reference element
document.body.insertBefore(newDiv, referenceNode);
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>6. Manipulate Element Attributes</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>You can easily get, set, or remove the attributes of an HTML element using the following methods:</p>
<ul>
<li><b><mark>getAttribute()</mark></b>: Retrieves the value of an attribute.</li>
<li><b><mark>setAttribute()</mark></b>: Sets a new value for an attribute.</li>
<li><b><mark>removeAttribute()</mark></b>: Removes an attribute.</li>
</ul>
<pre>
// Get the value of an attribute
let src = document.getElementById("image").getAttribute("src");
// Set a new value for an attribute
document.getElementById("image").setAttribute("src", "new-image.jpg");
// Remove an attribute
document.getElementById("image").removeAttribute("src");
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>7. Manipulate Data Attributes</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>HTML5 introduced data attributes, which are custom attributes that you can use to store extra
information about an element. These are particularly useful for adding data to an element
without affecting its visual structure.</p>
<ul>
<li><b><mark>dataset</mark></b>: A special property in JavaScript that allows you to access data attributes.</li>
</ul>
<pre>
// Setting a data attribute
document.getElementById("demo").dataset.userId = "12345";
// Getting a data attribute
let userId = document.getElementById("demo").dataset.userId;
console.log(userId); // Outputs: 12345
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h2 id="D02">02 How to Add a <b><mark>Class</mark></b> to <b><mark>DOM Element</mark></b> in JavaScript?</h2>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Adding a class to a <b><mark>DOM (Document Object Model)</mark></b> element in
JavaScript is a fundamental task that enables developers to dynamically manipulate
the appearance and behavior of web pages. Classes in HTML provide a powerful way to
apply CSS styles or JavaScript functionality to multiple elements at once.</p>
<p>By using JavaScript, you can easily add, remove, or toggle classes on elements, making
your web applications more interactive and responsive to user actions.</p>
<p>These are the following approaches:</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Table of Content</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ul>
<li>Using <b><mark>classList</mark></b> Property</li>
<li>Using <b><mark>className</mark></b> Property</li>
<li>Using <b><mark>classList</mark></b> Property</li>
</ul>
<p>In this approach, we are using <b><mark>classList</mark></b> property to add the class into the
<b><mark>DOM element</mark></b>. It returns the class name as a <b><mark>DOMTokenList</mark></b>
object. It has a method called “add” which is used to add class name to elements. We will access
the <b><mark>div</mark></b> using the <b><mark>getElementById</mark></b> and we will use the add property of
<b><mark>classList</mark></b> to add a class.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>element.classList.add("className")</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example: This example shows the implementation of the above-explained approach.</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html>
<head>
<style>
.geek {
background-color: green;
font-size: 50px;
}
</style>
</head>
<body>
<button onclick="myClass()">Try it</button>
<div id="gfg">Geeks for Geeks</div>
<script>
function myClass() {
let elem = document.getElementById("gfg");
// Adding class to div element
elem.classList.add("geek");
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~ 03. update heading font size, background color using class ~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a02-9ea4-7ea7-973f-f2abd1c889bf"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image003.gif?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Update heading font size, background color using class"
title="Update heading font size, background color using class"
alt="Update heading font size, background color using class." />
</a>
</p>
<!-- image003.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>Using <b><mark>className</mark></b> Property</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
In this approach, we are using the <b><mark>className</mark></b> property. This property returns the
<b><mark>className</mark></b> of the element. If the element has already a class then it will simply
add another one to it or else it will append our new class to it.
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>HTMLElementObject.className</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example: This example shows the implementation of the above-explained approach.</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html>
<head>
<style>
.geekClass {
background-color: green;
text-align: center;
font-size: 50px;
}
</style>
</head>
<body>
<div id="gfg">
Geeks For Geeks
</div>
<button onclick="myClassName()">Add Class</button>
<script>
function myClassName() {
let element = document.getElementById("gfg");
// Adding the class geekClass to element
// with id gfg space is given in className
// (" geekClass") as if there is already
// a class attached to an element than our
// new class won't overwrite and will append
// one more class to the element
element.className += " geekClass";
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~ 04. Update font size, background color using class ~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a02-9ea4-7ea7-973f-f2abd1c889bf"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image004.gif?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Update font size, background color using class"
title="Update font size, background color using class"
alt="Update font size, background color using class." />
</a>
</p>
<!-- image004.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h2 id="D03">03. How to select DOM Elements in JavaScript?</h2>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Selecting <b><mark>DOM (Document Object Model)</mark></b> elements is a fundamental aspect of web
development with JavaScript. It allows developers to interact with and manipulate elements
on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating
content, adding event listeners, or modifying styles.</p>
<p>Below are the approaches to select DOM elements in JavaScript:</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Table of Content</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ul>
<li><href id="#D03-01">1. Using <b><mark>getElementById</mark></b></a></li>
<li><href id="#D03-02">2. Using <b><mark>getElementsByClassName</mark></b></a></li>
<li><href id="#D03-03">3. Using <b><mark>getElementsByTagName</mark></b></a></li>
<li><href id="#D03-04">4. Using <b><mark>querySelector</mark></b></a></li>
<li><href id="#D03-05">5. Using <b><mark>querySelectorAll</mark></b></a></li>
</ul>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3 id="D03-01">1. Using <b><mark>getElementById</mark></b></h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>This method selects a single element by its unique ID attribute.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>document.getElementById('id')</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>getElementById Example</title>
</head>
<body>
<h1 id="gfg">GeeksForGeeks</h1>
<script>
// styling the h1 tag
const element = document.getElementById('gfg');
element.style.color = "green";
element.style.textAlign = "center";
element.style.margin = "30px";
element.style.fontSize = "30px";
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 05. GeeksForGeeks in green ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href=""
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image005.png?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Geeks4Geeks in green"
title="Geeks4Geeks in green"
alt="Geeks4Geeks in green." />
</a>
</p>
<!-- image005.png -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3 id="D03-02">2. Using <b><mark>getElementsByClassName</mark></b></h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>This method selects elements based on their class attribute. It returns a collection of elements
with the specified class name.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>document.getElementsByClassName('class')</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>getElementsByClassName Example</title>
</head>
<body>
<h1 class="selector">GeeksForGeeks</h1>
<h2 class="selector">DOM selector in JavaScript</h2>
<script>
const elements =
documSizeent.getElementsByClassName('selector');
elements[0].style.color = "green";
elements[1].style.color = "red";
elements[0].style.textAlign = "center";
elements[1].style.textAlign = "center";
elements[0].style.marginTop = "60px";
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~ 06. Geeks4Geeks in green, DOM select in JavaScript in red ~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a0c-76d2-77de-8fb5-d9b140c651d3"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image006.png?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Geeks4Geeks in green, DOM select in JavaScript in red"
title="Geeks4Geeks in green, DOM select in JavaScript in red"
alt="Geeks4Geeks in green, DOM select in JavaScript in red." />
</a>
</p>
<!-- image006.png -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3 id="D03-03">03. Using <b><mark>getElementsByTagName</mark></b></h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>This method selects elements based on their tag name. It returns a collection of elements with the
specified tag name.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>document.getElementsByTagName('tag')</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>getElementsByTagName Example</title>
</head>
<body>
<p>Thanks for visiting GFG</p>
<p>This is showing how to select DOM element using tag name</p>
<script>
const paragraphs = document.getElementsByTagName('p');
paragraphs[0].style.color = "green";
paragraphs[1].style.color = "blue";
paragraphs[0].style.fontSize = "25px";
paragraphs[0].style.textAlign = "center";
paragraphs[1].style.textAlign = "center";
paragraphs[0].style.marginTop = "60px";
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~ 07. first paragraph in green, 2nd in blue, centered, 60px from top ~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a0c-76d2-77de-8fb5-d9b140c651d3"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image007.png?raw=true"
loading="lazy"
style="width:40%;"
aria-label="1st paragraph in green, 2nd in blue, all centered, 1st paragraph 60px from top of form"
title="1st paragraph in green, 2nd in blue, all centered, 1st paragraph 60px from top of form"
alt="1st paragraph in green, 2nd in blue, all centered, 1st paragraph 60px from top of form." />
</a>
</p>
<!-- image007.png -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3 id="D03-04">04. Using <b><mark>querySelector</mark></b></h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>This method selects the first element that matches a specified CSS selector. It returns only
one element.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>document.querySelector('selector')</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>querySelector Example</title>
</head>
<body>
<div class="gfg">GeeksForGeeks</div>
<script>
const element =
document.querySelector('.gfg');
element.style.color = "green";
element.style.textAlign = "center";
element.style.margin = "30px";
element.style.fontSize = "30px";
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4.
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~ 08. Geeks4Geeks in green, centered, 30px from margin, 30px in size ~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a10-0251-74d1-adda-143743783632"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image008.png?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Geeks4Geeks in green, centered, 30px from margin, 30px in size"
title="Geeks4Geeks in green, centered, 30px from margin, 30px in size"
alt="Geeks4Geeks in green, centered, 30px from margin, 30px in size." />
</a>
</p>
<!-- image008.png -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3 id="D03-05">05. Using <b><mark>querySelectorAll</mark></b></h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Similar to <b><mark>querySelector</mark></b>, but it returns a <b><mark>NodeList</mark></b> containing
all elements that match the specified CSS selector.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>document.querySelectorAll('selector')</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>querySelectorAll Example</title>
</head>
<body>
<h1 class="selector">GeeksForGeeks</h1>
<p class="selector">
This is showing how to select DOM element
using tag name
</p>
<script>
const elements = document.querySelectorAll('.selector');
elements[0].style.color = "green";
elements[1].style.color = "red";
elements[0].style.textAlign = "center";
elements[1].style.textAlign = "center";
elements[0].style.marginTop = "60px";
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~ 09. how to select dom element using tag name ~~~~~~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a10-0251-74d1-adda-143743783632"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image009.png?raw=true"
loading="lazy"
style="width:40%;"
aria-label="How to select dom element using tag name"
title="How to select dom element using tag name"
alt="How to select dom element using tag name." />
</a>
</p>
<!-- image009.png -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h2 id="D04">04. How to get all ID of the DOM elements with JavaScript?</h2>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Given a HTML document and the task is to get the all ID of the DOM elements in an array. There are
two methods to solve this problem which are discusses below:</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>Approach 1:</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>First select all elements using <b><mark>$(‘*’)</mark></b> selector, which selects every element
of the document. Use <b><mark>.each()</mark></b> method to traverse all elements and check if it
has an ID. If it has an ID then push it into the array.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example: This example implements the above approach.</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to get all ID of the DOM
elements with JavaScript ?
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>
Click on the button to get
all IDs in an array.
</p>
<button onclick="muFunc()">
Click Here
</button>
<p id="GFG"></p>
<script>
let res = document.getElementById("GFG");
function muFunc() {
let ID = [];
$("*").each(function () {
if (this.id) {
ID.push(this.id);
}
});
res.innerHTML = ID;
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10. click to get all ids in an array ~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a10-0251-74d1-adda-143743783632"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image010.gif?raw=true"
loading="lazy"
style="width:30%;"
aria-label="Click to get all ids in array"
title="Click to get all ids in array"
alt="Click to get all ids in array." />
</a>
</p>
<!-- image010.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>Approach 2:</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<ul>
<li>First select all elements using <b><mark>$(‘*’)</mark></b> selector, which selects every
element of the Document.</li>
<li>Use <b><mark>.map()</mark></b> method to traverse all element and check if it has ID.</li>
<li>If it has ID then push it in the array using <b><mark>.get()</mark></b> method.</li>
</ul>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Example: This example implements the above approach.</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre>
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to get all ID of the DOM
elements with JavaScript ?
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>
Click on the button to get
all IDs in an array.
</p>
<button id="Geeks" onclick="muFunc()">
Click Here
</button>
<p id="GFG"></p>
<script>
let res = document.getElementById("GFG");
function muFunc() {
let ID = [];
ID = $("*").map(function() {
if (this.id) {
return this.id;
}
}).get();
res.innerHTML = ID;
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~~~~~~~ 11. click to get/display all ids in an array ~~~~~~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a13-68ce-7a88-870c-df992c440067"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image011.gif?raw=true"
loading="lazy"
style="width:30%;"
arial-label="Click to get/display all ids in an array"
title="Click to get/display all ids in an array"
alt="Click to get/display all ids in an array." />
</a>
</p>
<!-- image011.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h2 id="D05">05. JavaScript – How to Get the Data Attributes of an Element?</h2>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>Here are the various methods to get the data attributes of an element using JavaScript.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>1. Using dataset Property</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>The <b><mark>dataset</mark></b> property in JavaScript allows you to access all data attributes
of an HTML element as a <b><mark>DOMStringMap</mark></b> object. It simplifies retrieving, modifying,
or interacting with custom data stored in attributes like data-id or data-name.</p>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Syntax</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<pre><code>
const e = document.getElementByID('demo') // Accessing the element
const dataId = e.dataset.dataID //Access the data-id attribute
</code></pre>
<pre>
<!DOCTYPE html>
<html>
<body>
<div
id="element"
data-typeId="123"
data-name="name"
data-points="10"
data-important="true">
This is the Target Element.
</div>
<br />
<button onclick="myFunction()">
Get Attributes
</button>
<p id="result"></p>
<script>
let result = document.getElementById("result");
let e = document.getElementById("element");
function myFunction() {
let jsonData = JSON.stringify({
id: parseInt(e.dataset.typeid),
points: parseInt(e.dataset.points),
important: e.dataset.important,
dataName: e.dataset.name
});
result.innerHTML = jsonData;
}
</script>
</body>
</html>
</pre>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h4>Output:</h4>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<!--~~~~~~~~~~~~~~~~~ 12. click to get/display attributes for target elements ~~~~~~~~~~~~~~~~~~-->
<p align="center" >
<a href="https://codepen.io/editor/BBauska/pen/019e2a15-4bb7-7e90-b9bb-9f8ea163d394"
target="_blank" rel="noopener noreferrer">
<img class="displayed"
src="./images/image012.gif?raw=true"
loading="lazy"
style="width:40%;"
aria-label="Click to get/display attributes for target elements"
title="Click to get/display attributes for target elements"
alt="Click to get/display attributes for target elements." />
</a>
</p>
<!-- image012.gif -->
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<h3>2. Using getAttribute() Method</h3>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<p>The <b><mark>getAttribute()</mark></b> method in JavaScript retrieves the value of a specified
attribute from an HTML element. To get data attributes, use <b><mark>element.getAttribute(‘data-attribute’)</mark></b>.
This method allows precise selection and manipulation of individual data attributes without
accessing all at once.</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<div
id="target"
data-typeId="123"
data-name="name"
data-points="10"
data-important="true"
>
This is the Target Element.
</div>
<br />
<button onclick="myFunction()">Get Attributes</button>
<p id="result"></p>
<script>
let result = document.getElementById("result");
let e = document.getElementById("target");
function myFunction() {
let jsonData = JSON.stringify({