YouTip LogoYouTip

Htmldom Modify

HTML DOM Modification

HTML DOM Modification

-- Learning is not just about technology, but also about dreams!

HTML DOM Tutorial

HTML DOM Tutorial HTML DOM Introduction HTML DOM Nodes HTML DOM Methods HTML DOM Properties HTML DOM Access HTML DOM Modification HTML DOM Modify HTML Content HTML DOM Elements HTML DOM Events HTML DOM Navigation HTML DOM Summary DOM Examples

DOM Reference Manual

HTML DOM Objects

< Previous: HTML DOM Access Next: HTML DOM Modify HTML Content >

Deep Dive

  • Scripts
  • Programming Languages
  • Development Tools
  • Web Services
  • Scripting Languages
  • Web Design & Development
  • Computer Science
  • Software
  • Web Service
  • Programming

HTML DOM - Modification


Modifying HTML = changing elements, attributes, styles, and events.


Modifying HTML Elements

Modifying the HTML DOM means many different aspects:

  • Changing HTML content
  • Changing CSS styles
  • Changing HTML attributes
  • Creating new HTML elements
  • Removing existing HTML elements
  • Changing events (handlers)

In the following chapters, we will learn in depth the common methods for modifying the HTML DOM.


Creating HTML Content

The simplest way to change the content of an element is by using the innerHTML property.

The following example changes the HTML content of a <p> element:

Example

Hello World!

document.getElementById("p1").innerHTML="New text!";

The paragraph content was modified by script.

Try it yourself Β»

lamp We will explain the details in the example in the following chapters.


Changing HTML Style

Through the HTML DOM, you can access the style object of an HTML element.

The following example changes the HTML style of a paragraph:

Example

Hello world!

Hello world!

document.getElementById("p2").style.color="blue"; document.getElementById("p2").style.fontFamily="Arial"; document.getElementById("p2").style.fontSize="larger";

Try it yourself Β»


Creating New HTML Elements

To add a new element to the HTML DOM, you must first create the element (element node), and then append it to an existing element.

Example

This is a paragraph.

This is another paragraph.

var para=document.createElement("p"); var node=document.createTextNode("This is a new paragraph."); para.appendChild(node); var element=document.getElementById("div1"); element.appendChild(para);

Try it yourself Β»

← Event BindMet Table Createthead β†’