Altering html with javascript
Written by: maffelu , 2009-10-13 12:26:52
<body id="myBody"></body>
var body = document.getElementById("myBody");
body.innerHTML = "Some text in the body!";
<body id="myBody">Some text in the body!</body>
<html>
<head>
<script type="text/javascript">
function changeText() {
var header = document.getElementById("header");
var paragraph = document.getElementById("paragraph");
header.innerHTML = "A crappy header...";
paragraph.innerHTML = "A dull paragraph...";
};
</script>
<title>Our experiment</title>
</head>
<body>
<h1 id="header">A fantastic header</h1>
<p id="paragraph">A perilous paragraph</p>
<input type="submit" onclick="changeText()" />
</body>
</html>
<html>
<head>
<script type="text/javascript">
function changeClass() {
var paragraph = document.getElementById("paragraph");
paragraph.className = "p2";
};
</script>
<style type="text/css">
.p1{ font-weight: bold; }
.p2{ font-family: normal; }
</style>
<title>Our experiment</title>
</head>
<body>
<p id="paragraph" class="p1">I am Lorem Ipsum!</p>
<input type="submit" onclick="changeClass()" />
</body>
</html>
<html>
<head>
<script type="text/javascript">
function changeClass(newClass) {
var paragraph = document.getElementById("paragraph");
paragraph.className = newClass;
};
</script>
<style type="text/css">
.p1{ font-weight: bold; }
.p2{ font-family: normal; }
</style>
<title>Our experiment</title>
</head>
<body>
<p id="paragraph" class="p2" onmouseover="changeClass('p1');" onmouseout="changeClass('p2');">I am Lorem Ipsum!</p>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function loadImages(){
otherImage = new Image();
otherImage.src = "exampleButton2.png";
}
function changePicture(newPicture) {
var myImage = document.getElementById("myPicture");
myImage.src = newPicture;
};
</script>
<title>Our experiment</title>
</head>
<body onload="loadImages()">
<img id="myPicture" src="exampleButton1.png"
onmouseover="changePicture('exampleButton2.png')"
onmouseout="changePicture('exampleButton1.png')" />
</body>
</html>
<html>
<head>
<script type="text/javascript">
function changeLink(newUrl, newText) {
var myLink = document.getElementById("myLink");
myLink.href = newUrl;
myLink.innerHTML = newText;
};
</script>
<title>Our experiment</title>
</head>
<body onload="loadImages()">
<a href="http://www.google.com" id="myLink">Google</a>
<input type="submit" onclick="changeLink('http://www.yahoo.com', 'Yahoo'); return false;" />
</body>
</html>
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.