Wednesday, September 3, 2025
HomeLanguagesJavascriptTurn on or off bulb using JavaScript

Turn on or off bulb using JavaScript

Write a JavaScript code that turns the ON and OFF Light Bulb. We will define a function turnOnOff() that will change the bulb from ON to OFF and vice versa

Syntax:

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

Example: In this example, we will write a code that changes the state of bulb from ON to OFF and vice-versa by clicking on the image of bulb.

HTML




<script>
    function turnOnOff() {
        var 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>


Output: 

 

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS