Open In App

Turn on or off bulb using JavaScript

In this article, we are going to learn how to turn on or off bulbs using JavaScript.

Syntax

<img src = "URl" onClick=turnOnOff()>

Approach

Example: In this example, We are following the above-explained approach.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <script>
        function turnOnOff() {
            let image = document.getElementById('Image');
            if (image.src.match("ONbulb"))
                image.src =
            else
                image.src =
        }
    </script>
 
    <img id="Image" onclick="turnOnOff()" src=
 
    <p>
        Click on the bulb to turn it ON and OFF
    </p>
</body>
 
</html>

Output: 




Article Tags :