Skip to content
GitHub

Working with Attributes

Image ရဲ့ src ပြောင်းမလား? Link ရဲ့ href ပြောင်းမလား? getAttribute, setAttribute နဲ့ removeAttribute တို့ကို သုံးနိုင်ပါတယ်။

1. getAttribute (တန်ဖိုး ယူမယ်)

Section titled “1. getAttribute (တန်ဖိုး ယူမယ်)”

Attribute တစ်ခုရဲ့ တန်ဖိုးကို လိုချင်ရင် သုံးပါတယ်။

let link = document.querySelector("a");
let url = link.getAttribute("href");
console.log(url); // "https://google.com"

2. setAttribute (တန်ဖိုး အသစ်သတ်မှတ်မယ်)

Section titled “2. setAttribute (တန်ဖိုး အသစ်သတ်မှတ်မယ်)”

ရှိပြီးသား တန်ဖိုးကို ပြင်ချင်ရင် (သို့) အသစ်ထည့်ချင်ရင် သုံးပါတယ်။

let img = document.querySelector("img");
// ပုံ ပြောင်းလိုက်မယ်
img.setAttribute("src", "new-image.png");

3. removeAttribute (ဖျက်မယ်)

Section titled “3. removeAttribute (ဖျက်မယ်)”

Attribute တစ်ခုလုံးကို ဖျက်ပစ်ချင်ရင် သုံးပါတယ်။

let btn = document.querySelector("button");
// disabled="true" ကို ဖျက်လိုက်ရင် ခလုတ်နှိပ်လို့ ရသွားမယ်
btn.removeAttribute("disabled");

Direct Property Access (ပိုလွယ်သော နည်းလမ်း)

Section titled “Direct Property Access (ပိုလွယ်သော နည်းလမ်း)”

Standard attribute အများစုကို . ခံပြီး တိုက်ရိုက် ခေါ်လို့လည်း ရပါတယ်။

img.src = "pic.jpg";
input.value = "Hello";
link.href = "https://facebook.com";