Leakbali
w3 Tutorial, Web Tutorial
Switch to English Bahasa Indonesia 
Register   Login

HTML DOM - How To


HTML elements can be changed using JavaScript, the HTML DOM and events. 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:

How To Change an HTML Element Example

<html>
<body>

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

</body>
</html> 
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:

Change the Text of an HTML Element using innerHTML Example

<html>
<body>

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

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

</body>
</html> 
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.

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

Change an HTML Element Using Events Example

<html>
<body>

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

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

Change the Text of an Element With a Function 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> 

Tampilan Bagaimana Merubah Teks pada Sebuah Elemen Menggunakan Fungsi

Text changed here!

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:

Using the Style Object 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> 

Hot To Using the Style Object Display

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

How To Change the Font and Color of an Element Example

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

<body>
<p id="p1">Welcome Leakbali.com Visitors!</p>
<input type="button" onclick="ChangeStyle()" value="Change style" />
</body>
</html> 

How To Change the Font and Color of an Element Display

Welcome Leakbali.com Visitors!

Tutorial


References

About Us

Home
About Us
Contact Us
Sitemap

Tools

Google PageRank
Alexa Rank
Keywords Density

Accounts

Register Account
Login
Valid XHTML 1.0 TransitionalValid CSS!
Web Directory


2006 - 2012 © Leakbali.com - Free Web Tutorial, Free Web Articles, Web Sharing, Source Codes, Web References