w3schools
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About

HTML DOM - Change HTML Elements

« Previous Next Chapter »

HTML elements can be changed using JavaScript, the HTML DOM and events.


Change an HTML Element

HTML DOM and JavaScript can change the inner content and attributes of HTML elements.

The following example changes the background color of the <body> element:

Example

<html>
<body>

<script type="text/javascript">
document.body.bgColor="yellow";
</script>

</body>
</html>

Try it yourself »


Change the Text of an HTML Element - innerHTML

The easiest way to get or modify the content of an element is by using the innerHTML property.

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

Example

<html>
<body>

<p id="p1">Hello World!</p>

<script type="text/javascript">
document.getElementById("p1").innerHTML="New text!";
</script>

</body>
</html>

Try it yourself »


Change an HTML Element Using Events

An event handler allows you to execute code when an event occurs.

Events are generated by the browser when the user clicks an element, when the page loads, when a form is submitted, etc.

You can read more about events in the next chapter.

The following example changes the background color of the <body> element when a button is clicked:

Example

<html>
<body>

<input type="button" onclick="document.body.bgColor='lavender';"
value="Change background color" />

</body>
</html>

Try it yourself »


Change the Text of an Element - with a Function

The following example uses a function to change the text of the <p> element when a button is clicked:

Example

<html>
<head>
<script type="text/javascript">
function ChangeText()
{
document.getElementById("p1").innerHTML="New text!";
}
</script>
</head>

<body>
<p id="p1">Hello world!</p>
<input type="button" onclick="ChangeText()" value="Change text" />
</body>
</html>

Try it yourself »


Using the Style Object

The Style object of each HTML element represents its individual style.

The following example uses a function to change the style of the <body> element when a button is clicked:

Example

<html>
<head>
<script type="text/javascript">
function ChangeBackground()
{
document.body.style.backgroundColor="lavender";
}
</script>
</head>

<body>
<input type="button" onclick="ChangeBackground()"
value="Change background color" />
</body>
</html>

Try it yourself »


Change the font and color of an Element

The following example uses a function to change the style of the <p> element when a button is clicked:

Example

<html>
<head>
<script type="text/javascript">
function ChangeStyle()
{
document.getElementById("p1").style.color="blue";
document.getElementById("p1").style.fontFamily="Arial";
}
</script>
</head>

<body>
<p id="p1">Hello world!</p>
<input type="button" onclick="ChangeStyle()" value="Change style" />
</body>
</html>

Try it yourself »

« Previous Next Chapter »


Altova® XMLSpy® - The industry-leading XML editor!

Altova XMLSpy

Whether you're new to XML or already an advanced user, the user-friendly views and powerful entry helpers, wizards, and debuggers in XMLSpy are designed to meet your XML and Web development needs from start to finish. New features in Version 2010!

  • XML editor
  • Graphical XML Schema / DTD editors
  • XSLT 1.0/2.0 editor, debugger, profiler
  • XQuery editor, debugger, profiler
  • XBRL validator, taxonomy editor, taxonomy wizard
  • Support for Office Open XML (OOXML)
  • Graphical WSDL 1.1/2.0 editor & SOAP debugger
  • JSON editing & conversion
  • Java, C#, C++ code generation
  • And much more!

Download a free trial today!

  Altova XMLSpy


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Top Web Hosting
Cheap Web Hosting
WEB BUILDING
Download XML Editor
FREE Flash Website
FREE Web Templates
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
SHARE THIS PAGE