DOM Selectors နဲ့ Input Events

ECMAScript 2026

Element အများကြီးကို တစ်ခါတည်း ရွေးပြီး စာရိုက်ချိန်၊ တန်ဖိုး အတည်ပြုချိန်နဲ့ key နှိပ်ချိန်ကို ဖမ်းကြည့်ပါ။

ဒီစာမျက်နှာမှာ

querySelectorAll() နဲ့ Node အများကြီး ရွေးခြင်း

CSS selector ပေးရင် ကိုက်ညီတဲ့ node အားလုံးကို static NodeList အဖြစ် ရပါတယ်။ forEach() နဲ့ တစ်ခုစီ လုပ်နိုင်ပါတယ်။ Node အသစ် ထည့်ရင် အရင်ရထားတဲ့ list က လိုက်မပြောင်းပါဘူး။

စမ်းသပ်နည်း ဒီနမူနာကို .html ဖိုင်အသစ်အဖြစ် သိမ်းပြီး browser နဲ့ ဖွင့်ပါ။ ပြင်ပြီးတိုင်း save နဲ့ reload လုပ်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Select many nodes with querySelectorAll()</title>
</head>
<body>
<ul><li class="product">Pen</li><li class="product">Notebook</li></ul>
  <script>
const products = document.querySelectorAll(".product");
products.forEach(product => {
  product.textContent += " ✓";
});
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ List item နှစ်ခုလုံးမှာ အမှန်ခြစ် ပေါ်ပါမယ်။ Selector ကိုက်တဲ့ item တတိယတစ်ခု HTML ထဲ ထည့်ပြီး Run ပါ။

input Event နဲ့ ရိုက်သမျှ ပြောင်းလဲမှု ဖမ်းခြင်း

User က input ရဲ့ တန်ဖိုး ပြင်တဲ့အခါ input event ဖြစ်ပါတယ်။ လက်ရှိ value ကို ဖတ်ပြီး စာလုံးရေ ဒါမှမဟုတ် preview ကို ချက်ချင်း ပြောင်းနိုင်ပါတယ်။

စမ်းသပ်နည်း ဒီနမူနာကို .html ဖိုင်အသစ်အဖြစ် သိမ်းပြီး browser နဲ့ ဖွင့်ပါ။ ပြင်ပြီးတိုင်း save နဲ့ reload လုပ်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Handle every edit with input</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
  <script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("input", () => {
  result.textContent = note.value.length + " characters";
});
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ စာရိုက်နေတုန်း စာလုံးရေ လိုက်ပြောင်းတာ မြင်ရပါမယ်။

change Event နဲ့ တန်ဖိုး အတည်ပြုတာ ဖမ်းခြင်း

Text field မှာ စာပြင်ပြီး field ကနေ focus ထွက်တဲ့အခါ change ဖြစ်လေ့ရှိပါတယ်။ Select မှာတော့ option အသစ်ရွေးတဲ့အခါ ဖြစ်ပါတယ်။

စမ်းသပ်နည်း ဒီနမူနာကို .html ဖိုင်အသစ်အဖြစ် သိမ်းပြီး browser နဲ့ ဖွင့်ပါ။ ပြင်ပြီးတိုင်း save နဲ့ reload လုပ်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>React to a committed value with change</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
  <script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("change", () => {
  result.textContent = "Saved: " + note.value;
});
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ မတူတဲ့ စာရိုက်ပြီး field အပြင်ကို နှိပ်ပါ။ သိမ်းလိုက်တဲ့ တန်ဖိုး ပေါ်ပါမယ်။

keydown Event နဲ့ နှိပ်လိုက်တဲ့ Key ကို ဖတ်ခြင်း

Key နှိပ်တဲ့အခါ keydown ဖြစ်ပါတယ်။ event.key မှာ ဘယ် key နှိပ်လဲ သိနိုင်လို့ Enter လို key တစ်ခုကို သီးသန့် တုံ့ပြန်နိုင်ပါတယ်။ စာသားကို ပြင်တာ စောင့်ဖို့ input event ကို သုံးပါ။

စမ်းသပ်နည်း ဒီနမူနာကို .html ဖိုင်အသစ်အဖြစ် သိမ်းပြီး browser နဲ့ ဖွင့်ပါ။ ပြင်ပြီးတိုင်း save နဲ့ reload လုပ်ပါ။

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Read a pressed key with keydown</title>
</head>
<body>
<label for="note">Order note</label>
<input id="note">
<p id="result" aria-live="polite"></p>
  <script>
const note = document.querySelector("#note");
const result = document.querySelector("#result");
note.addEventListener("keydown", event => {
  if (event.key === "Enter" && !event.isComposing) {
    result.textContent = "Confirmed: " + note.value;
  }
});
  </script>
</body>
</html>

စမ်းကြည့်ရင် ဘာဖြစ်မလဲ Field ထဲ စာရိုက်ပြီး Enter နှိပ်ရင် စာကို အတည်ပြုပေးပါမယ်။

တရားဝင် ကိုးကားချက်များ