Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Turn on or off bulb using JavaScript

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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: 

 

My Personal Notes arrow_drop_up
Last Updated : 13 Dec, 2022
Like Article
Save Article
Similar Reads