By using the JavaScript user will get the behavior of the elements. With the help of JavaScript user can do the many changes to the html documents. Few examples are demonstrated below.
The below code demonstrates the use of JavaScript and user can change the content of the html document.
[html]
<!DOCTYPE html>
<html>
<body>
<h1>SPLESSONS</h1>
<p>JavaScript can change the content of an HTML element:</p>
<button type="button" onclick="myFunction()">Click Me!</button>
<p id="demo">SPlessons Introduction</p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello splessons!";
}
</script>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
When the user click on the above button then the content of the document will change as shown in below image.
The code below demonstrates changing the style of the document by using the java script.
[html]
<!DOCTYPE html>
<html>
<body>
<h1>SPLESSONS</h1>
<p id="demo">Welcome to SPlessons.</p>
<script>
function myFunction() {
document.getElementById("demo").style.fontSize = "25px";
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
When the user click on the above button the document style get changed as shown in below image.
The code below demonstrates changing the images by using the JavaScript.
[html]
<!DOCTYPE html>
<html>
<body>
<script>
function light(sw) {
var pic;
if (sw == 0) {
pic = "splessons.JPG"
} else {
pic = "splessons1.JPG"
}
document.getElementById('myImage').src = pic;
}
</script>
<img id="myImage" src="splessons1.JPG" width="300" height="250">
<p>
<button type="button" onclick="light(1)">Next</button>
<button type="button" onclick="light(0)">Prev</button>
</p>
</body>
</html>
[/html]
Result
By running the above code in a preferred browser user can get the following output as shown in below image.
When the user click on the above buttons then user can get the next image as shown in below image.