Content changes in Html Dom
# Dom_ javascript
Changes to HTML content using HTML DOM
Content changes in HTML using the HTML DOM (Document Object Model) are primarily done through JavaScript by accessing the desired element and modifying its properties. Here are the most commonly used methods:
1. Text Contents
Use the .textContent or .innerText feature to change text within an HTML element:
.textContent: Set or return the text content of all descendants and elements, ignoring HTML formatting (e.g. < br > tags).
.innerText: Sets or returns the "visible" text content of an element. It takes into account CSS formatting (i.e. excludes text hidden by display: none;)
Example:
HTML
< p id = "myParagraph" > Original text < / p >
< script >
/ / Select the < p > element with id.
const pElement = document.getElementById ("myParagraph");
/ / Change text content
pElement.textContent = "New text that has been changed by DOM";
/ / or pElement.innerText = "New text changed by DOM";
< / script >
2. Changing HTML content (HTML Content)
Use the .innerHTML feature to change all HTML content within the element. This can include other HTML tags:
Example:
HTML
< div id = "myDiv" >
< p > Original content < / p >
< / div >
< script >
/ / Select the < div > element with id.
Const DivElement = document.getElementById ("myDiv");
/ / Change content to new HTML (include < strong > and < em > tags)
DivElement.innerHTML = "< h2 > New topic < / h2 > < p > < strong > text was < em > highlighted < / em > < / strong > < / p >";
< / script >
Caution: Using .innerHTML with data coming from untrusted users can cause XSS (Cross-Site Scripting) vulnerabilities. .textContent should be used only when changing text, for security purposes.
3. Changing the properties (Attributes) of the elements
Use the .setAttribute () method or direct property access to change the value of the: attribute.
Example:
HTML
< img id = "myImage" src = "old _ image .jpg" alt = "original image" >
< script >
const imgElement = document.getElementById ("myImage");
/ / 1.Change the 'src' attribute directly
imgElement.src = "new _ image.png";
/ / 2.Change the 'alt' attribute using setAttribute
imgElement.setAttribute ("alt," "new image");
/ / 3. Change the 'class' attribute
imgElement.classname = "active-state";
< / script >
Summary of content change procedures:
Search (Find): Use DOM methods such as document.getElementById (), document.querySelector (), document.getElementsByClassName () to select the target element.
Change (Change): Use features such as .textContent, .innerHTML, .src, .setAttribute () to edit the content or properties of selected elements.
DOM คืออะไร (อธิบายแบบคนเริ่มทำเว็บ) ตอนเราเริ่มเขียนเว็บใหม่ ๆ คำว่า “DOM” โผล่มาบ่อยมาก โดยเฉพาะเวลาอยากทำให้หน้าเว็บเปลี่ยนข้อความ เปลี่ยนรูป หรือกดปุ่มแล้วมีอะไรเกิดขึ้น สรุปสั้น ๆ DOM (Document Object Model) คือ “โครงสร้างของหน้าเว็บ” ที่เบราว์เซอร์สร้างขึ้นมาจาก HTML ของเรา แล้วเปิดให้ JavaScript เข้ามาอ่าน/แก้ไขได้เหมือนเป็นต้นไม้ (tree) ของ node และ element ทำไมต้องเข้าใจ DOM? เพราะเวลาพูดว่า “เปลี่ยนหน้าเว็บด้วย JavaScript” จริง ๆ คือเราไม่ได้ไปแก้ไฟล์ HTML โดยตรง แต่เราไปแก้ DOM ที่กำลังแสดงอยู่ในเบราว์เซอร์ เช่น เปลี่ยนข้อความใน <p>, ใส่ HTML ใหม่ใน <div>, หรือเปลี่ยน attribute ของ <img> ภาพรวมขั้นตอนที่ใช้บ่อย (จำง่าย ๆ) 1) Find: หา element ที่ต้องการ - document.getElementById("...") - document.querySelector("...") (แนะนำเพราะใช้ selector แบบ CSS) 2) Change: เปลี่ยนสิ่งที่ต้องการ - เปลี่ยนข้อความ: .textContent หรือ .innerText - เปลี่ยนโค้ด HTML ข้างใน: .innerHTML - เปลี่ยน attribute: .setAttribute() หรือเปลี่ยน property ตรง ๆ เช่น img.src ตัวอย่างที่เจอบ่อยในงานจริง - อยากอัปเดตข้อความสถานะ เช่น “กำลังโหลด...” -> “เสร็จแล้ว” ผมจะใช้ textContent เป็นหลัก เพราะปลอดภัยและไม่ต้องสนใจแท็ก HTML: const status = document.querySelector('#status'); status.textContent = 'เสร็จแล้ว'; - อยากใส่โครงสร้าง HTML เพิ่ม เช่น สร้างรายการหรือหัวข้อย่อย อันนี้ค่อยใช้ innerHTML แต่ต้องมั่นใจว่า string ไม่ได้มาจากผู้ใช้แบบสุ่ม ๆ: const box = document.querySelector('.result'); box.innerHTML = '<h2>ผลลัพธ์</h2><p>อัปเดตแล้ว</p>'; ข้อควรระวังที่หลายคนพลาด: innerHTML และ XSS ถ้าคุณเอาข้อความจากช่อง input หรือข้อมูลที่ผู้ใช้ส่งมา แล้วยัดเข้า innerHTML ตรง ๆ อาจโดนฝังสคริปต์ (XSS) ได้ ทางที่ปลอดภัยคือ: - ถ้าเป็น “ข้อความล้วน” ใช้ textContent - ถ้าจำเป็นต้องสร้าง element ให้ใช้ document.createElement() + appendChild() (ปลอดภัยกว่า innerHTML) สรุปสั้น ๆ ให้จำ DOM คือสะพานระหว่าง HTML กับ JavaScript: เรา “หา element” ใน DOM แล้ว “แก้เนื้อหา/คุณสมบัติ” ด้วย textContent, innerText, innerHTML, และ setAttribute() ถ้าเน้นปลอดภัยให้เริ่มจาก textContent ก่อน แล้วค่อยขยับไปใช้ innerHTML เมื่อจำเป็นจริง ๆ




































































































































