JavaScript: Elements by Tag name in DOM | getElementsByTagName | HTMLCollection #kody_az

Dərs haqqında

document.getElementsByTagName() JavaScript DOM API-də bir funksiyadır və verilən sənəddə (və ya müəyyən bir elementi daxilində) müəyyən bir HTML teqi ilə uyğun gələn bütün elementləri geri qaytarır. Bu funksiya sizə həmin teq adı ilə yaradılmış canlı (HTMLCollection) tipli bir kolleksiya təqdim edir. “Canlı” kolleksiya dedikdə, DOM-da dəyişiklik etdikdə (məsələn, yeni element əlavə etdikdə və ya sildikdə) kolleksiyanın avtomatik olaraq yenilənməsi nəzərdə tutulur.

Əsas Xüsusiyyətlər:
1. HTMLCollection qaytarır:
getElementsByTagName()-in qaytardığı kolleksiya canlıdır və avtomatik yenilənir.

2. Teq Adlarında Hərf Böyüklüyü Həssaslığı (HTML üçün):
HTML sənədlərində teq adlarında hərf böyüklüyü nəzərə alınmır. Yəni getElementsByTagName("div") və getElementsByTagName("DIV") eyni nəticəni verəcək. Lakin XML sənədlərində (məsələn, XHTML, XML) teq adlarında hərf həssaslığı mövcuddur.

3. Global və ya Lokal İstifadə:
document.getElementsByTagName("p") bütün sənəddəki 'p' elementlərini geri qaytaracaq.

4. Nəticələrdə gəzinti (Iterasiya):
Qaytarılan HTMLCollection massivə bənzəsə də, o, bir massiv deyil. İterasiya üçün ənənəvi for döngəsindən və ya for...of döngəsindən istifadə edə bilərsiniz. Əgər massiv metodlarından (forEach, map, filter və s.) istifadə etmək istəyirsinizsə, onu əsl massivə çevirmək lazımdır.

========

The document.getElementsByTagName() method is a built-in function in the JavaScript DOM API that returns a live HTMLCollection of all elements within the document (or within a specified element) that match a particular HTML tag name. The returned list updates automatically as the document’s structure changes.

Key Points about getElementsByTagName():
1. Returns an HTMLCollection:
Unlike methods like querySelectorAll(), which return a static NodeList, getElementsByTagName() returns a “live” collection. This means if the document structure changes after you call it (for example, if you add or remove elements), the returned collection will reflect those changes in real time.

2. Case-insensitivity in HTML documents:
In HTML documents, tag names are generally case-insensitive. document.getElementsByTagName("div") and document.getElementsByTagName("DIV") produce the same result. However, for XML documents (including XHTML served with an XML MIME type), the tag name lookup is case-sensitive.

3. Iterating Over the Result:
Because getElementsByTagName() returns an HTMLCollection, you can access elements by index or iterate through them, usually converting them into an array if needed, since HTMLCollection does not have array methods like forEach by default.

#kody_az #kodyaz #js #javascript #nodejs #es6 #viral #viralcode #nodejs #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz

YouTube-da bax · KODY.AZ