JavaScript: createElement in DOM | append appendChild #kody_az

Dərs haqqında

JavaScript-də DOM ilə işləyərkən tez-tez istifadə olunan iki əsas metod var document.createElement() və document.createDocumentFragment(). Hər ikisi DOM-düyünlər (node) yaratmaq üçündür, lakin fərqli məqsədlər daşıyır. Bu metodların istifadəsi, kodunuzu daha səmərəli və oxunaqlı etməyə kömək edəcək.

document.createElement(tagName) müəyyən bir HTML elementi yaratmaq üçün istifadə olunur. Bu üsul yeni elementi hələ DOM-a (səhifəyə) əlavə etmədən sadəcə yaddaşda (memory) yaradır. Həmin element yalnız appendChild() və ya append() kimi metodlarla əlavə edildikdə real DOM-un bir hissəsinə çevrilir.

document.createDocumentFragment() metodu “yüngül” bir konteyner yaradır. Bu konteyner adi tag kimi DOM-da görünmür, amma onun daxilində bir çox elementi yaddaşda toplu şəkildə hazırlayıb, daha sonra hamısını bir yerdə DOM-a əlavə etmək mümkündür.

Fragment Haqqında Vacib Məlumatlar
• Fragment görünməzdir, öz “etiketi” yoxdur.
• İçinə istənilən tipdə düyün (element, mətn, şərh və s.) əlavə edə bilərsiniz.
• Fragment DOM-a əlavə edildikdə, fragment “boşalır” və yalnız uşaqları (child nodes) DOM-a keçir.

======

When manipulating the DOM (Document Object Model) in JavaScript, two commonly used methods for creating new DOM nodes are:
1. document.createElement()
2. document.createDocumentFragment()

They serve different (but related) purposes, and understanding each will help you write more efficient and organized code.

The createElement() method is used to create a single HTML element node. This newly created element exists in memory until you append it into the DOM.

createDocumentFragment() creates a lightweight “container” or “mini-document” that can hold multiple nodes. Unlike regular elements , a DocumentFragment is not rendered directly to the page. Instead, it is used as a temporary container to batch-manipulate or build DOM structures in memory before inserting them into the main document.

Key Points About Fragments
• A fragment cannot be styled directly (it’s invisible).
• You can attach any type of node to it (elements, text nodes, comments, etc.).
• When you append a fragment to the DOM, the fragment itself disappears, leaving only its child nodes in the DOM.

#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