JavaScript ተጠቅሞ HTML ን ቀጥታ ለውጥ! 🔥
Use JavaScript to dynamically change, add, and remove HTML elements!
DOM = Document Object Model — Browser HTML ን ዛፍ (tree) ሆኖ ያዘጋጃል። JavaScript ደግሞ ያ tree ላይ ነገሮችን ሊጨምር፣ ሊቀይር፣ ሊሰርዝ ይችላል!
The DOM is how browsers represent HTML as a tree structure. JS can then access and modify any part of that tree!
DOM = HTML ዛፍ ነው። document = ሙሉ page, document.body = <body> tag, getElementById = አንድ element ማግኘት
Think of DOM as a family tree — document is the root, html is the trunk, head/body are branches, elements are leaves.
🌳 DOM Tree — HTML ዛፍ:
Elements ለማግኘት (Selecting):
| Method | ምን ያደርጋል? | ምሳሌ |
|---|---|---|
| getElementById() | id ተጠቅሞ አንድ element Get element by id | getElementById("title") |
| querySelector() | CSS selector ተጠቅሞ Get first matching element | querySelector(".card") |
| querySelectorAll() | ሁሉም matching elements Get all matching elements | querySelectorAll("p") |
Elements ለመቀየር (Modifying):
| Property/Method | ምን ያደርጋል? | ምሳሌ |
|---|---|---|
| .innerText | ጽሑፍ ለማንበብ/ለመቀየር Get/set text content | el.innerText = "አዲስ" |
| .innerHTML | HTML ጽሑፍ ለመቀየር Get/set HTML content | el.innerHTML = "<b>ወፍራም</b>" |
| .style | CSS ቀጥታ ለመቀየር Change CSS directly | el.style.color = "red" |
| .classList | CSS class ለመጨምር/ለማስወጣት Add/remove CSS classes | el.classList.add("active") |
| createElement() | አዲስ element ለመስራት Create new HTML element | createElement("div") |
| appendChild() | Element ለመጨመር Add element to DOM | parent.appendChild(el) |
| .remove() | Element ለማስወጣት Remove element from DOM | el.remove() |
// Element ማግኘት const title = document.getElementById("myTitle"); // ጽሑፍ ለውጥ title.innerText = "አዲስ ርዕስ! 🎉"; // Style ለውጥ title.style.color = "#e879f9"; title.style.fontSize = "36px"; // Class ጨምር title.classList.add("highlight"); // አዲስ element ስራ const newP = document.createElement("p"); newP.innerText = "አዲስ paragraph! 🆕"; document.body.appendChild(newP); // Event listener — ጠቅ ሲደረግ const btn = document.getElementById("myBtn"); btn.addEventListener("click", function() { alert("Button ተጫነ! ✅"); });
🎮 Demo 1 — Live Page Builder: ጽሑፍ ጻፍ → Page ላይ ይጨምር!
Type text and add it live to the page using DOM!
👆 ከላይ ጽሑፍ ጽፈህ "Page ላይ ጨምር" ጫን...
🎮 Demo 2 — Event Listeners: ጠቅ አድርግ፣ hover አድርግ — ክስተቱ ይመዝገብ!
See different DOM events in action — click, hover, double-click:
Click, Double-click, ወይም Hover ሞክር
🎮 Demo 3 — Todo App (DOM ሙሉ ምሳሌ):
A full todo app using DOM — add, complete, and delete tasks!
Browser HTML ን tree ሆኖ ያዘጋጃል — JS ያ tree ን ይቆጣጠራል።
The DOM is the browser's tree representation of HTML — JS accesses it via document.
Element ለማግኘት — id ተጠቅሞ ወይም CSS selector ተጠቅሞ።
Use getElementById for id, querySelector for CSS selectors.
ጽሑፍ፣ HTML ወይም CSS ለመቀየር — element ካገኘህ በኋላ።
Change text with innerText, HTML with innerHTML, CSS with .style.
አዲስ element ስርተህ page ላይ ለመጨምር።
Create new elements with createElement, add them with appendChild.
click, keypress, mouseover... ክስተቶችን ለማዳመጥ addEventListener ይጠቀሙ።
Use addEventListener to react to user events like clicks and keypresses.