Automatically translated.View original post

Content changes in Html Dom

# Web Site Writing Basics

# javascript

# 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.

2025/10/28 Edited to

... Read moreDOM คืออะไร (อธิบายแบบคนเริ่มทำเว็บ) ตอนเราเริ่มเขียนเว็บใหม่ ๆ คำว่า “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 เมื่อจำเป็นจริง ๆ

Related posts

Free certifications that shine on your LinkedIn
1. AWS Cloud Practitioner Essentials (Free Intro Course) - What It Covers: Cloud computing basics, AWS services, and pricing models. - Why It’s Valuable: Provides foundational knowledge in cloud computing and AWS. 2. Microsoft Azure Fundamentals (Free Learning Path) - What It C
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

2201 likes

These sites saved my brain in 2025 🧠💻
1. ChatHub I used to bounce between AI platforms, copy-pasting the same prompt over and over to compare answers. With ChatHub, I can talk to multiple AIs at the same time, in one clean interface. It’s honestly my go-to whenever I’m brainstorming content, writing study scripts, or just curious whi
emilie.studygram

emilie.studygram

563 likes

A pink mechanical keyboard with glowing keys and colorful string lights, overlaid with text that reads "LEARN SOFTWARE DEV FOR FREE 6 RESOURCES," indicating a guide to free software development learning.
A guide titled "1. LEARN THE INTERNET" for beginners, listing key concepts like HTTPS and DNS. It recommends "Front End Masters - Sections 1-4" and shows the cover of "The Front End Developer/Engineer Handbook 2024."
A guide titled "2. LEARN HTML/CSS" for beginners, highlighting key concepts like web page structure and CSS Flexbox. It recommends "FreeCodeCamp - Responsive Web Design Course" and displays a screenshot of the course page.
Study to be a Frontend Dev - Roadmap (100% Free)
This is a study guide for becoming a Front End Developer! All resources mentioned are FREE. Don’t pay anyone for this info! Why am I not recommending a zero to job course? Two Reasons 1-There is no course that will teach you everything 2-You need to put in some sweat equity, only wat
Study Seal

Study Seal

688 likes

An image of stacked books with loose pages, featuring the title "free books in PDF, Kindle, ePub, and other formats" in white text.
A bookshelf filled with various books, overlaid with text detailing "1. Public Domain & Classic Literature" and information about Project Gutenberg, including formats and content.
A vintage green book on a wooden surface, with text describing "2. Library eBook Services" and details about Libby/OverDrive, including formats and content.
free books in PDF, Kindle, ePub, and other formats
📚 1. Public Domain & Classic Literature ✅ Project Gutenberg • Formats: PDF, ePub, Kindle, HTML, Plain text • Content: Over 70,000 free eBooks (e.g., Shakespeare, Jane Austen, Mark Twain) • Use: Totally legal; public domain books ⸻ 📖 2. Library eBook Servic
Ivonne Evangelista

Ivonne Evangelista

860 likes

Roadmap to Becoming a Frontend Developer
Want to break into frontend development but don’t know where to start? 🤔 This step-by-step roadmap will guide you from beginner to job-ready frontend developer! #codingforbeginners #htmlcssforbeginners #programming #studymotivations #softwareengineer
Aysha

Aysha

52 likes

HTML5 Cheatsheet for Beginners – Essential Tags
Want to master HTML5? Here’s a quick cheatsheet covering essential HTML tags – from document structure to forms, tables, and multimedia elements! #coding #html #programming #studymotivation
Aysha

Aysha

106 likes

HTML5 Structure: A Visual Guide for Beginners
#html5 #webdevelopment #learntocode #codingforbeginners #SemanticHTML
Aysha

Aysha

148 likes

Core HTML Elements
Core HTML Elements HTML structures websites for readability, using elements like headers, paragraphs, lists, and embedded content. Headers (h1 - h6): HTML headings (h1-h6) define content hierarchy. H1 is the main heading, while h2-h6 are subheadings, each deeper than the last. Paragraphs (
ZeroandoneHQ

ZeroandoneHQ

0 likes

HTML <br> Tag Explained in 60 Seconds
"Day 15 of Tag a Day with Aysha! Today we’re learning the <br> tag — the simplest way to create line breaks in HTML. Perfect for addresses, poems, and text formatting. Follow along for quick HTML lessons every day! #codingforbeginners #htmlforbeginners #brtag #codewithaysha
Aysha

Aysha

2 likes

@Firmoo Glasses 🤓 https://www.firmoo.com/eyeglasses-p-13167.html?color=38066 #UP_win =2267&content=8
Belenybooo

Belenybooo

3 likes

HTML and Css Fundamentals.
1. Core HTML Elements: Defines HTML as the "skeleton" of a website, explaining the use of h1 to h6 for headings, <p> for paragraphs, and styling tags like <strong> for importance, <em> for emphasis, <sub> for subscripts, and <sup> for superscripts. 2. Lists
ZeroandoneHQ

ZeroandoneHQ

1 like

The image is a title slide for a guide on common HTML mistakes and their fixes. It features an illustration of a person coding on a laptop, surrounded by code snippets and language labels like HTML, CSS, and C++. The text encourages avoiding errors to write clean HTML.
This slide addresses the mistake of forgetting the `<!DOCTYPE html>` declaration. It shows incorrect HTML code without it and the correct version including it, explaining that the doctype ensures proper browser rendering.
The slide highlights the error of not using semantic HTML. It contrasts using generic `<div>` tags for everything with the correct approach of using semantic tags like `<header>` and `<section>`, emphasizing improved SEO and accessibility.
Common HTML Mistakes Beginners Make and How to Fix
#learntocode #studymotivation #success #html #webdevelopment
Aysha

Aysha

22 likes

HTML Tags You Didn’t Know Exist
#htmlforbeginners #codingjourney #codingforbeginners #webdesigntips #softwareengineering
Aysha

Aysha

32 likes

Day in the life of 🩺🌡️
A Day in the Life of a Medical Assistant Student This week’s focus: OB/GYN, Pediatrics, and Reproductive System Disorders—and wow, what a week it’s been! From learning about prenatal care and female reproductive health to practicing pediatric assessments and diving into conditions like PCOS a
✝️AftenSkyy ✝️

✝️AftenSkyy ✝️

6 likes

A dark blue graphic titled "TAG A DAY WITH AYSHA" for Day 19, introducing HTML list tags `<ul>`, `<ol>`, `<li>`. It encourages making content clear and organized with HTML Lists, featuring a code icon and "Code With Aysha" branding.
A dark blue graphic explaining `<ul>`, `<ol>`, `<li>` tags with HTML code examples for a shopping list (unordered) and steps to code (ordered). It defines `<ul>` as unordered (bullets), `<ol>` as ordered (numbers), and `<li>` as a list item.
A dark blue graphic titled "Why It Matters," illustrating the benefits of HTML lists with icons for scannable content, mobile UX, and consistent design. Text highlights improved readability, structure for content, and semantic meaning.
HTML Lists Made Simple: <ul>, <ol>, & <li> Explain
Lists are everywhere — shopping lists, steps to follow, features to highlight. In HTML, you can create them with just three tags: <ul> for unordered (bulleted) lists <ol> for ordered (numbered) lists <li> for each list item These tags don’t just organize content, they
Aysha

Aysha

3 likes

Watermelon 🍉 Detox 1st Day
Watermelon 🍉 Detox 1st Day! And I will say it hasn’t been bad..I just eat it when I get hungry. You don’t have to starve yourself. It’s just allowing the body tim to cut back on higher calories. Stop cravings for processed foods and Benefits of watermelon 🍉 A study conducted at the Unive
Debs point of view

Debs point of view

85 likes

1/17 THIS IS LITERALLY TAXPAYER - FUNDED PROPAGANDA!!!!! -https://fortune.com/2026/01/08/former-ice-director-wartime-recruitment-bonus-officer-training-pay/   -https://www.washingtonpost.com/technology/2025/12/31/ice-wartime-recruitment-push/   -https://www.theguardian.com/us-news/2025/dec/31/
Jaydin🦋

Jaydin🦋

2 likes

Thrift jackpot that changes the whole room
Some things are doing too much. Once they’re gone, the room settles. Let me help you make it real. https://www.flippingtheflip.com/p/start-here.html The $100 Room Glow-up Guide #interiordesign #homedecor #designinspo #designtips #postitdesigntips
Becky @ Flipping the Flip

Becky @ Flipping the Flip

24 likes

Congress’s ability to fulfill its constitutional role is indeed severely diminished as of the end of 2025. The U.S. Constitution assigns Congress critical powers: making laws, appropriating funds (the "power of the purse"), overseeing the executive branch, and representing the people in the legisla
Celeste 🌺

Celeste 🌺

3 likes

A title slide for 'TAG A DAY WITH AYSHA' featuring the <span> tag, described as a 'tiny but mighty tool for styling specific words in a sentence,' for Day 14 of 'Code With Aysha'.
Explains what the <span> tag is, comparing plain and styled text with a code example showing 'purple' styled. It defines <span> as an inline, non-semantic HTML element for styling content parts.
Highlights why the <span> tag matters, showing examples of text and listing its benefits: styling without breaking flow, targeting specific words, and adding interactivity with JavaScript.
Day 14: HTML <span> Tag Explained
Learn how to use the HTML <span> tag to style and target specific words or phrases without breaking your layout. Perfect for beginners learning HTML and CSS! #HTML #WebDevelopment #LearnToCode #HTMLforBeginners #codewithaysha
Aysha

Aysha

2 likes

This image introduces "HTML + CSS IN ACTION: 30 Days of Mini Projects," focusing on Day 14: "Two-Column Layout (Sidebar + Main Content)." It highlights organizing webpages with a clean two-column design, presented by Code With Aysha.
This image defines a two-column layout, showcasing an example with a sidebar for navigation and a main content area. It explains that the sidebar is for navigation/extra info, and the main content is for articles/posts, built using HTML and CSS.
This image explains the benefits of a two-column layout, including organization, scannability, structuring real-world websites, and responsive design. A diagram illustrates sidebar and content alignment within the layout.
How to Build a Two-Column Layout with HTML + CSS
Learn how to create a clean, responsive two-column layout using only HTML and CSS! This project shows you how to combine a sidebar and main content area using Flexbox — perfect for dashboards, blogs, and portfolios. #HTML #CSS #Flexbox #WebDesign #codewithaysha
Aysha

Aysha

5 likes

The image introduces "HTML + CSS IN ACTION: 30 Days of Mini Projects Modal Popup," showcasing a modal example with text "This is a Modal" and an "Open Modal" button. It highlights "Pop up power—no JavaScript needed" for Day 23, by Code With Aysha.
This image defines a modal popup as a small overlay that captures user attention and "locks" the screen. It displays a side panel with "Quick Settings" including Dark Mode, Font Size, Accent Color, Animations, Notifications, and Quick Links.
The image explains why building modals with HTML + CSS matters, demonstrating a "Page" and an "Overlay + Centered Card" modal. It emphasizes learning CSS selectors, visibility control, layering, transitions, and responsive centering without JavaScript.
Build a Sleek Slide-in Side Panel with HTML CSS
Build a modern slide-in side panel using only HTML and CSS! Perfect for quick settings, theme controls, and navigation links. Fully responsive and beginner-friendly — no JavaScript required. #HTMLCSS #WebDesign #FrontendDevelopment #codewithaysha
Aysha

Aysha

5 likes

Build a Modern Blog Preview Card with HTML & CSS |
Today we’re building a clean blog post preview card: 🖼 Image 📝 Title 📝 Short text snippet This layout is used in blogs, portfolios, and news websites — perfect for practicing real-world UI design. You’ll learn: ✨ Semantic HTML structure 🎨 Card layout & spacing in CSS 🪄 Hover + shado
Aysha

Aysha

4 likes

Semantic HTML
Did you know that using Semantic HTML makes your website more accessible, improves SEO, and enhances user experience? #webaccessibility #semanticHTML #webdevelopment #webdesign #frontenddevelopment
Aysha

Aysha

4 likes

The image introduces a 'Modern Testimonial Slider' project, part of '30 Days of Mini Projects' using HTML and CSS. It features a sample testimonial card from Michael Johnson and highlights bringing UI to life with sliding testimonials, marking it as Day 28 with Code With Aysha.
This image defines testimonial sliders as rotating carousels for customer reviews. It showcases an example testimonial from Ana Garcia and lists benefits like saving space, modern appearance, adding social proof, and enhancing UI movement.
The image explains the importance of testimonial sliders, featuring a sample testimonial from John Doe. It lists essential applications for portfolio websites, business landing pages, product showcases, course pages, and agency sites, emphasizing their role in building trust.
How to Build a Testimonial Slider Using HTML & CSS
Day 28 — Modern Testimonial Slider Today we’re building a clean, responsive testimonial slider using only HTML + CSS. Smooth animations, auto-slide effects, and a modern card UI — perfect for portfolios, business websites, and landing pages #webdevelopment #carouselsliders #codewithaysha
Aysha

Aysha

5 likes

HTML Anniversary Carol
Well, HTML began in 1991 with Tim-Berners-Lee drafting what became HTML; and 34 years, 32 years and 30 years on; HTML 1.0, HTML 2.0, HTML 3.0 onwards will continue to evolve how forms, tables, graphics and structural tags are supported. Happy 34 years, Happy 32 years and Happy 30 years to HTML
Benjamin Tuckett

Benjamin Tuckett

0 likes

A promotional image for 'TAG A DAY WITH AYSHA' on Day 20, introducing the HTML tags <dl>, <dt>, and <dd> to structure knowledge with clarity, from 'Code With Aysha'.
An educational image defining the HTML tags <dl>, <dt>, and <dd>. It shows a code snippet example and explains <dl> as a definition list container, <dt> as a definition term, and <dd> as a definition description.
An informational image explaining why definition lists matter, highlighting their benefits for organizing terms, suitability for glossaries, FAQs, and product features, and their role in improving accessibility and SEO.
What Are Definition Lists in HTML?
Learn how to use the <dl>, <dt>, and <dd> tags in HTML. Not all lists are the same! In this video, we explore definition lists—the perfect way to pair terms with their meanings. Whether it’s glossaries, FAQs, or product specs, you’ll see when to use each tag, how they boost readab
Aysha

Aysha

4 likes

Glowing Newsletter Form with HTML + CSS
Day 12 of my 30 Days of Mini Projects series — we’re building a stylish newsletter subscription form! It’s simple, modern, and perfect for dark-theme websites. You’ll learn how to style glowing inputs, gradient buttons, and a soft animated glow effect using CSS only. #HTMLCSS #WebDesign
Aysha

Aysha

1 like

This image introduces Day 2 of "HTML + CSS IN ACTION: 30 Days of Mini Projects," focusing on "Buttons with Hover Effects." It emphasizes making buttons interactive with CSS, presented by Code With Aysha.
This image illustrates the importance of hover effects by comparing a plain button to one with a hover glow. It explains that hover effects make website buttons interactive and engaging.
This image presents code for creating a button with hover effects. It includes basic HTML for the button and CSS styles for its appearance, along with a purple background and scale transform on hover.
Day 2: Buttons with Hover Effects | HTML + CSS
Welcome to Day 2 of HTML + CSS in Action! 🚀 Today we’re transforming a plain HTML button into an interactive button with hover effects using CSS. A simple touch that makes your websites feel alive and professional! #HTMLCSS #CodingForBeginners #MiniProjects #hoverbutton #codewithaysha
Aysha

Aysha

8 likes

A cover image for an HTML + CSS project titled 'Parallax Sunset Mountains', featuring a stylized sunset with dark mountains and trees, highlighting it as 'Day 27' of mini-projects by Code With Aysha.
An explanation of the parallax effect, defining it as layers moving at different speeds during scrolling to create depth, motion, and a realistic 3D look using pure CSS, with an example of mountains.
An illustration detailing the anatomy of a parallax scene, showing sun/sky as the back layer, distant mountains as the mid layer, trees as the front layer, and birds as foreground animation.
Parallax Sunset Mountains (HTML + CSS Project)
In this mini-project, we create a beautiful parallax landscape using pure HTML + CSS — no JavaScript needed. The scene includes: 🌞 glowing sunset ⛰️ layered mountains 🌲 dark forest silhouette #HTMLCSS #Parallax #WebDesign #codewithaysha
Aysha

Aysha

4 likes

See more