ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
DOM tree ထဲက element ကို မပြောင်းခင် အရင်ဆုံး ရှာရပါတယ်။ Selector method တွေက လိပ်စာ ပေးပြီး လူရှာတာနဲ့ တူပါတယ်: getElementById က မှတ်ပုံတင်နံပါတ်နဲ့ ရှာသလို တစ်ယောက်တည်းကို တိုက်ရိုက် ရှာပါတယ်။ querySelector ကတော့ "အပြာရောင် အင်္ကျီဝတ်ထားတဲ့ ပထမဆုံးလူ" လို ဖော်ပြချက် (CSS selector) နဲ့ ရှာပါတယ်။
querySelectorAll ကတော့ ဖော်ပြချက်နဲ့ ကိုက်တဲ့ လူအားလုံးရဲ့ စာရင်းကို ပြန်ပေးပါတယ်။ ရှာမတွေ့ရင် querySelector က null ပြန်ပြီး querySelectorAll က အလွတ်စာရင်း ပြန်ပါတယ် — ဒီကွာခြားချက်က bug အများကြီးရဲ့ အရင်းအမြစ်ပါ။
Problem → Why → Solution
ပြဿနာ: selector မှားတာ သေးသေးလေးက error ကြီးဖြစ်
အောက်က line သုံးကြောင်းစလုံး ကြည့်ရတာ မှန်ပုံရပေမယ့် တစ်ကြောင်းမှ အလုပ်မဖြစ်ပါဘူး။ ပထမ နှစ်ကြောင်းက error မပေးဘဲ null ကို ငြိမ်ငြိမ်လေး ပြန်ပြီး နောက်တစ်ကြောင်းကျမှ "Cannot read properties of null" ဆိုပြီး ပေါက်ကွဲပါတယ်။ တတိယကြောင်းက "map is not a function" ဆိုတဲ့ TypeError ပေးပါတယ်၊ querySelectorAll က array ပြန်တယ်လို့ ထင်ထားလို့ပါ။
// HTML: <div id="menu"><li class="dish">Mohinga</li>...</div>
const menu = document.getElementById("#menu"); // null: no # in getElementById
const first = document.querySelector("dish"); // null: looks for a <dish> tag
const names = document.querySelectorAll(".dish")
.map((li) => li.textContent); // TypeError: map is not a function
console.log(first.textContent); // TypeError: Cannot read properties of nullဘာကြောင့် မှားတာလဲ
getElementById က id စာသားကိုပဲ လက်ခံလို့ "#menu" ဆိုရင် id က "#menu" ဖြစ်တဲ့ element ကို ရှာပါတယ်။ querySelector က CSS selector လက်ခံလို့ class အတွက် အစက် (.) လိုပါတယ်၊ "dish" ဆိုရင် <dish> tag ကို ရှာပါတယ်။ querySelectorAll က array မဟုတ်ဘဲ NodeList ပြန်ပြီး NodeList မှာ forEach ရှိပေမယ့် map မရှိပါဘူး။ Method တစ်ခုချင်း ဘာလက်ခံလဲ၊ ဘာပြန်လဲ သိရင် ဒီအမှားတွေ မဖြစ်တော့ပါဘူး။
Core concept: method တစ်ခုစီ ဘာလက်ခံပြီး ဘာပြန်လဲ
နောက် lesson တွေက DOM ပြောင်းလဲမှု တိုင်းဟာ ဒီလို ရှာဖွေမှုနဲ့ စပါတယ်၊ ဒါကြောင့် "ကျွန်တော့် code က ဘာမှ မလုပ်ဘူး" ဆိုတဲ့ ပြဿနာ အများစုရဲ့ အရင်းက ရှာတာ မှားတာပါ။ Method ကို ရွေးတဲ့အခါ မေးခွန်း နှစ်ခု မေးပါ: element တစ်ခုလား အများကြီးလား၊ DOM ပြောင်းရင် လိုက်ပြောင်းတဲ့ live list လိုလား။
| Method | ပြန်ရတာ | မတွေ့ရင် |
|---|---|---|
| getElementById("menu") | Element တစ်ခု | null |
| querySelector("#menu .dish") | ပထမဆုံး ကိုက်တဲ့ element | null |
| querySelectorAll(".dish") | Static NodeList | အလွတ် NodeList (length 0) |
| getElementsByClassName("dish") | Live HTMLCollection | အလွတ် collection |
getElementById က id စာသားကို လက်ခံပြီး id တွေက page တစ်ခုထဲမှာ unique ဖြစ်ရမှာမို့ element တစ်ခုပဲ ပြန်ပါတယ်။ querySelector နဲ့ querySelectorAll ကတော့ stylesheet ထဲမှာ ရေးတဲ့ CSS selector အတိုင်း လက်ခံပါတယ်: #id၊ .class၊ tag နာမည်၊ [data-id="3"] လို attribute selector၊ "#menu li" လို descendant selector နဲ့ ".dish.veg" လို class နှစ်ခုလုံးပါတဲ့ selector တွေပါ။ querySelector က document order အတိုင်း ပထမဆုံး ကိုက်တဲ့ element ကို ပြန်ပါတယ်။
Method တွေကို document ပေါ်မှာသာမက element တိုင်းပေါ်မှာ ခေါ်လို့ရပါတယ်။ menu.querySelectorAll("li") က menu ရဲ့ အောက်က li တွေကိုပဲ ရှာလို့ page ရဲ့ တခြားနေရာက li တွေ မပါလာပါဘူး၊ ပိုမြန်ပြီး ပိုတိကျပါတယ်။ (getElementById ကတော့ document ပေါ်မှာပဲ ရှိပါတယ်။)
data-* attribute တွေကို selector မှာ [data-id="3"] လို့ ရေးပြီး JavaScript ထဲမှာ element.dataset နဲ့ ဖတ်ပါတယ်။ Attribute နာမည်က dash နဲ့ ဆက်ထားရင် (data-stock-level) dataset မှာ camelCase (dataset.stockLevel) ဖြစ်သွားပါတယ်။ dataset ကနေ ရလာတဲ့ value အားလုံးက string ဖြစ်လို့ ဂဏန်းအဖြစ် တွက်ချက်ချင်ရင် Number() နဲ့ အရင် ပြောင်းရပါတယ်။
querySelectorAll ပြန်တဲ့ NodeList က array နဲ့ ဆင်ပေမယ့် array မဟုတ်ပါဘူး။ length၊ index နဲ့ forEach ရှိပေမယ့် map၊ filter၊ reduce မရှိလို့ [...list] ဒါမှမဟုတ် Array.from(list) နဲ့ array အဖြစ် အရင်ပြောင်းရပါတယ်။ Element တစ်ခုကို variable ထဲ သိမ်းထားပြီး ပြန်သုံးရင် ထပ်ခါထပ်ခါ ရှာစရာ မလိုပါဘူး၊ ဒါပေမယ့် innerHTML နဲ့ အဲဒီ node ကို အစားထိုးလိုက်ရင် သိမ်းထားတဲ့ reference က page ပေါ်မှာ မရှိတော့တဲ့ node အဟောင်းကို ညွှန်နေမှာ သတိထားပါ။
- Selector ကို CSS ပုံစံနဲ့ ရေး
- Scope ကျဉ်းအောင် parent ကနေ ရှာ
- null / length 0 ကို စစ်
- List ကို array ပြောင်းပြီး သုံး
Element တစ်ခုတည်း ရှာတဲ့ method တွေက null ပြန်နိုင်ပြီး အများကြီး ရှာတဲ့ method တွေက အလွတ်စာရင်း ပြန်ပါတယ် — ဘယ်တော့မှ undefined မပြန်ပါဘူး။
အထွေထွေ ပုံစံ
const one = document.getElementById("id-without-hash"); // element | null
const first = root.querySelector("css selector"); // element | null
const all = root.querySelectorAll("css selector"); // static NodeList
if (first === null) {
// handle the missing element
}
const values = [...all].map((element) => element.dataset.someKey);root က document ဒါမှမဟုတ် ရှာမယ့် နယ်ပယ်ကို ကျဉ်းစေတဲ့ element တစ်ခု ဖြစ်နိုင်ပါတယ်။ Element တစ်ခုတည်း ရှာရင် null ကို အမြဲ စစ်ပြီး အများကြီး ရှာရင် spread နဲ့ array အဖြစ် ပြောင်းပါ။ data-some-key attribute ကို dataset.someKey (camelCase) နဲ့ ဖတ်ပါတယ်။
Example: menu တစ်ခုထဲမှာ ရှာခြင်း
ဒီ code က ရှာမယ့် menu HTML ကို ကိုယ်တိုင် ဆောက်လို့ blank page တစ်ခုရဲ့ console ထဲမှာ တိုက်ရိုက် run လို့ ရပါတယ်။ Selector method တိုင်းကို တစ်ကြိမ်စီ သုံးထားပြီး မရှိတဲ့ element ကို ရှာရင် ဘာရလဲဆိုတာကိုလည်း နောက်ဆုံး line မှာ ပြထားပါတယ်။
// Build the HTML we will search (paste into the console of a blank page).
const app = document.createElement("div");
app.id = "menu";
app.innerHTML = `
<h2 class="title">Today's menu</h2>
<ul>
<li class="dish" data-id="1">Mohinga</li>
<li class="dish veg" data-id="2">Tea leaf salad</li>
<li class="dish" data-id="3">Shan noodles</li>
</ul>`;
document.body.append(app);
const menu = document.getElementById("menu");
const firstDish = document.querySelector("#menu .dish");
const vegDishes = document.querySelectorAll("#menu .dish.veg");
const allDishes = menu.querySelectorAll("li");
const noodles = menu.querySelector('[data-id="3"]');
console.log(menu.id);
console.log(firstDish.textContent);
console.log(vegDishes.length, vegDishes[0].textContent);
console.log([...allDishes].map((li) => li.dataset.id).join(","));
console.log(noodles.textContent);
console.log(document.querySelector("#missing"));Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- By id
const menu = document.getElementById("menu");# မပါဘဲ id စာသားကိုပဲ ပေးပါတယ်။ id က unique ဖြစ်လို့ element တစ်ခုတည်း ပြန်ရပါတယ်။
- First match
const firstDish = document.querySelector("#menu .dish");"#menu ရဲ့ အောက်က .dish class ပါတဲ့ element" လို့ ဆိုလိုပါတယ်။ ကိုက်တာ သုံးခုရှိပေမယ့် ပထမဆုံး Mohinga ကိုပဲ ပြန်ပါတယ်။
- Compound class selector
const vegDishes = document.querySelectorAll("#menu .dish.veg");.dish.veg (space မပါ) က class နှစ်ခုလုံး ပါတဲ့ element ကို ဆိုလိုလို့ Tea leaf salad တစ်ခုပဲ ကိုက်ပါတယ်။ ".dish .veg" (space ပါ) ဆိုရင် .dish ရဲ့ အောက်က .veg ကို ရှာမှာဖြစ်လို့ ဘာမှ မတွေ့ပါဘူး။
- NodeList to array
console.log([...allDishes].map((li) => li.dataset.id).join(","));NodeList မှာ map မရှိလို့ spread နဲ့ array အဖြစ် ပြောင်းပါတယ်။ dataset.id က data-id attribute ကို string အဖြစ် ဖတ်ပါတယ်။
- Attribute selector
const noodles = menu.querySelector('[data-id="3"]');menu ပေါ်မှာ ခေါ်ထားလို့ menu အတွင်းမှာပဲ ရှာပြီး data-id က "3" ဖြစ်တဲ့ element ကို ရွေးပါတယ်။
- No match
console.log(document.querySelector("#missing"));ကိုက်တဲ့ element မရှိလို့ null ပြန်ပါတယ်။ ဒီ null ပေါ်မှာ .textContent ခေါ်ရင် TypeError ဖြစ်မှာပါ။
menu
Mohinga
1 Tea leaf salad
1,2,3
Shan noodles
nullLive HTMLCollection နဲ့ static NodeList
getElementsByClassName နဲ့ getElementsByTagName က live HTMLCollection ပြန်ပါတယ် — DOM ပြောင်းတာနဲ့ collection က အလိုအလျောက် update ဖြစ်ပါတယ်။ querySelectorAll ကတော့ ခေါ်လိုက်တဲ့ အချိန်က snapshot (static NodeList) ကို ပြန်ပြီး နောက်မှ element ထပ်ထည့်လည်း မပြောင်းပါဘူး။ Live collection ကို loop ပတ်ရင်း element ဖယ်ရင် index တွေ ရွှေ့သွားပြီး တချို့ကို ကျော်သွားတတ်လို့ သတိထားပါ။
const list = document.createElement("ul");
list.innerHTML = "<li class='item'>A</li><li class='item'>B</li>";
document.body.append(list);
const live = list.getElementsByClassName("item"); // HTMLCollection (live)
const snapshot = list.querySelectorAll(".item"); // NodeList (static)
const extra = document.createElement("li");
extra.className = "item";
extra.textContent = "C";
list.append(extra);
console.log(live.length); // 3: updates by itself
console.log(snapshot.length); // 2: still the old snapshotအပေါ်ကို ရှာခြင်း: closest() နဲ့ matches()
querySelector က element ရဲ့ အောက်ဘက် (descendant) ကို ရှာပါတယ်။ တခါတလေ ဆန့်ကျင်ဘက် လိုပါတယ်: button တစ်ခုကနေ သူ့ကို ပိုင်တဲ့ list item ကို ရှာတာမျိုးပါ။ element.closest("li") က element ကိုယ်တိုင်ကနေ စပြီး parent တွေဆီ တက်ရှာကာ ပထမဆုံး ကိုက်တဲ့ ancestor ကို ပြန်ပါတယ် (မတွေ့ရင် null)။ element.matches(".veg") ကတော့ element တစ်ခုက selector နဲ့ ကိုက်လား true/false ပဲ ပြန်ပါတယ်။ ဒီ method နှစ်ခုက DOM Events lesson မှာ event delegation အတွက် အဓိက ကိရိယာတွေ ဖြစ်လာပါမယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Todo app တစ်ခုမှာ form၊ input နဲ့ list element တွေကို script စတဲ့အချိန်မှာ တစ်ကြိမ်ပဲ ရှာပြီး variable တွေထဲ သိမ်းထားလေ့ ရှိပါတယ်။ အဲဒီ element တွေ မရှိရင် app က ဘာမှ လုပ်လို့မရလို့ requireElement() လို helper နဲ့ "Missing element: #todo-form" ဆိုတဲ့ ရှင်းလင်းတဲ့ error ကို ချက်ချင်း ပေးတာက နောက်မှ null error တွေ ရှာနေရတာထက် အများကြီး သက်သာပါတယ်။
Designer တွေက styling အတွက် class နာမည်တွေကို မကြာခဏ ပြောင်းပါတယ်။ JavaScript behaviour ကို .btn-red လို presentation class နဲ့ ချိတ်ထားရင် CSS refactor တစ်ခုက feature ကို ပျက်စေနိုင်ပါတယ်။ ဒါကြောင့် team အများစုက data-action="delete"၊ data-id="42" လို data-* attribute တွေကို JavaScript hook အဖြစ် သုံးပါတယ်။ Playwright၊ Testing Library လို test tool တွေကလည်း class နာမည်ထက် data-testid လို stable attribute ဒါမှမဟုတ် role နဲ့ ရှာဖို့ အကြံပြုပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. getElementById မှာ # ထည့်တာ၊ querySelector မှာ . မေ့တာ
getElementById("#menu") နဲ့ querySelector("dish") နှစ်ခုလုံး error မပေးဘဲ null ပြန်ပါတယ်။ getElementById မှာ id စာသားပဲ ပေးပြီး querySelector မှာ CSS အတိုင်း #id နဲ့ .class ရေးပါ။
2. null ကို မစစ်ဘဲ property သုံးတာ
Page တချို့မှာပဲ ရှိတဲ့ element ကို ရှာရင် တခြား page တွေမှာ null ဖြစ်ပြီး script တစ်ခုလုံး ရပ်သွားပါတယ်။ မဖြစ်မနေ လိုတဲ့ element ဆိုရင် ရှင်းလင်းတဲ့ error throw လုပ်ပါ၊ optional ဆိုရင် if (el) သို့မဟုတ် el?.remove() နဲ့ ကာကွယ်ပါ။
3. NodeList ကို array လို သုံးတာ
querySelectorAll(...).map() က "map is not a function" error ပေးပါတယ်။ forEach ကို တိုက်ရိုက် သုံးလို့ရပေမယ့် map၊ filter၊ reduce အတွက် [...list] ဒါမှမဟုတ် Array.from(list) နဲ့ အရင်ပြောင်းပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Element တစ်ခုတည်း လိုရင် querySelector နဲ့ id selector ကို၊ အများကြီး လိုရင် querySelectorAll ကို default အဖြစ် သုံးပါ၊ selector ပုံစံ တစ်မျိုးတည်းနဲ့ ရေးလို့ ဖတ်ရလွယ်ပါတယ်။
- Script စတဲ့အချိန်မှာ အရေးကြီး element တွေကို တစ်ကြိမ်ပဲ ရှာပြီး နာမည်ကောင်းတဲ့ const variable တွေထဲ သိမ်းပါ။
- body > div > div > ul လို ရှည်လျားတဲ့ structural selector တွေ ရှောင်ပါ၊ HTML နည်းနည်း ပြောင်းတာနဲ့ ပျက်ပါတယ်။
Mini challenge
Mini challenge: ဆိုင်က stock ရှိတဲ့ ပစ္စည်းတွေ
Product list တစ်ခုကို ဆောက်ပြီး data-* attribute တွေနဲ့ stock ရှိတဲ့ item တွေကို ရွေးကာ နာမည်နဲ့ စုစုပေါင်း ဈေးနှုန်းကို print ထုတ်ပါ။ မရှိတဲ့ element ကို ရှာရင် ရှင်းလင်းတဲ့ error ပေးတဲ့ helper လည်း ရေးပါ။
- Pen (3, in)၊ Lamp (12, out)၊ Mug (7, in) ပါတဲ့ <ul class="products"> ကို ဆောက်ပါ (data-price နဲ့ data-stock သုံး)
- requireElement(selector, root) က မတွေ့ရင် "Missing element: ..." error throw လုပ်ရမယ်
- [data-stock="in"] selector နဲ့ stock ရှိတာတွေကို ရွေးပြီး array အဖြစ် ပြောင်းပါ
- dataset.price ကို Number() နဲ့ ပြောင်းပြီး ပေါင်းပါ
// TODO: build a <section> holding <ul class="products"> with three <li> items
// (data-price and data-stock attributes) and append it to document.body
function requireElement(selector, root = document) {
// TODO: querySelector inside root; throw an Error naming the selector if null
}
// TODO: find .products, then its [data-stock="in"] items as an array
// TODO: print "In stock: ..." and "Total price: ..."
// TODO: show the error message for requireElement(".cart", section)ရလဒ်: Console မှာ ဒီလို ပေါ်ရပါမယ်: In stock: Pen, Mug Total price: 10 Missing element: .cart
အရိပ်အမြွက် ကြည့်ရန်
dataset က string တွေ ပြန်လို့ "3" + "7" က "37" ဖြစ်ပါတယ် — ပေါင်းခင် Number() နဲ့ ပြောင်းပါ။ querySelectorAll ရလဒ်ကို [...] နဲ့ အုပ်ရင် map နဲ့ reduce သုံးလို့ရပါတယ်။
အဖြေ ကြည့်ရန်
// Builds its own elements, so it works on any blank page.
const shop = document.createElement("section");
shop.innerHTML =
'<ul class="products">' +
'<li data-price="3" data-stock="in">Pen</li>' +
'<li data-price="12" data-stock="out">Lamp</li>' +
'<li data-price="7" data-stock="in">Mug</li>' +
"</ul>";
document.body.append(shop);
function requireElement(selector, root = document) {
const element = root.querySelector(selector);
if (element === null) {
throw new Error(`Missing element: ${selector}`);
}
return element;
}
const list = requireElement(".products", shop);
const inStock = [...list.querySelectorAll('[data-stock="in"]')];
const names = inStock.map((li) => li.textContent);
const total = inStock.reduce((sum, li) => sum + Number(li.dataset.price), 0);
console.log(`In stock: ${names.join(", ")}`);
console.log(`Total price: ${total}`);
try {
requireElement(".cart", shop);
} catch (error) {
console.log(error.message);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- getElementById က # မပါတဲ့ id နဲ့ ရှာပြီး querySelector / querySelectorAll က CSS selector အပြည့်အစုံ လက်ခံပါတယ်။
- Element တစ်ခုတည်း ရှာတဲ့ method တွေက null ပြန်နိုင်လို့ အမြဲ စစ်ပါ၊ အများကြီး ရှာတဲ့ method တွေက အလွတ်စာရင်း ပြန်ပါတယ်။
- querySelectorAll က static NodeList ဖြစ်ပြီး getElementsBy... က live HTMLCollection ပါ၊ map သုံးဖို့ [...list] နဲ့ ပြောင်းပါ။
- element.querySelector() နဲ့ ရှာမယ့် နယ်ပယ်ကို ကျဉ်းပြီး JavaScript hook အတွက် data-* attribute တွေကို သုံးပါ။
MDN — Document Object Model (DOM) — MDN Web Docs