SimpleTuts.com

Functions in JavaScript

In JavaScript, you can run multiple statements using functions by defining a function that contains the statements you want to execute.


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Functions in JavaScript</title>
</head>

<body>
    <h1 id="demo">Hello Friends</h1>
    <p id="para">How is JavaScript?</p>
    <img src="nature1.webp" alt="" width="500" id="mypic">
    <br><br>
    <button onclick="changeAll();">Change All</button>
    <button onclick="newChanges();">New Changes</button>
    <script>
        function changeAll() {
            document.getElementById('demo').innerHTML = 'Welcome to JavaScript';
            document.getElementById('para').innerHTML = 'It is really fun'; 
            document.getElementById('mypic').src = 'nature2.webp';
        }
        function newChanges(){
            document.getElementById('mypic').width='700';
            document.getElementById('demo').style.color='red';
            document.getElementById('demo').style.backgroundColor='yellow';
            document.getElementById('demo').style.fontSize='50px';
        }
    </script>
</body>
</html>
Run Code