Turn on or off bulb using JavaScriptReadDiscussCoursesPracticeImprove Article ImproveSave Article SaveLike Article LikeWrite 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 versaSyntax:<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="https://media.geeksforgeeks.org/wp-content/uploads/OFFbulb.jpg"; else image.src="https://media.geeksforgeeks.org/wp-content/uploads/ONbulb.jpg"; }</script> <img id="Image" onclick="turnOnOff()" src="https://media.geeksforgeeks.org/wp-content/uploads/OFFbulb.jpg"> <p> Click on the bulb to turn it ON and OFF</p>Output: Last Updated : 13 Dec, 2022Like Article Save Article Please Login to comment...