Javascript Home

Javascript Example 1: Change background color

When you select the button it will change the background color and let you know about it.

The background of this page is white


HTML:


<!DOCTYPE html>
<html>
    <head>
        <script src="myscripts.js"></script>
        <title> Javascript example </title>
    </head>
    <body>
        <h1> Javascript Example 1: Change background color </h1>
        <button type="button" onclick="yellow()"> change background to yellow </button>
        <p aria-live="assertive" id="color"> The background of this page is white </p>
    </body>
</html>

Javascript:



function yellow() {
    document.body.style.backgroundColor = "yellow" ;
    document.getElementById("color").innerHTML = "background is now yellow!" ;
    
}


    
    
    

Next: Javascript example 2
Previous: Javascript Basics