Featured Article

W3Schools JavaScript Bulb Example Tutorial-Learn Lighting Effects

Adam Jul 13, 2026

When it comes to learning JavaScript, W3Schools often serves as a go-to online resource for developers of all levels, thanks to its intuitive and user-friendly approach. One of the engaging examples it provides is the interactive bulb on/off toggle using JavaScript, which serves as a perfect starting point for beginners to grasp the basics of this popular programming language.

Light Bulb 0n and Off Example Using HTML,CSS, JavaScript
Light Bulb 0n and Off Example Using HTML,CSS, JavaScript

This bulb example introduces fundamental JavaScript concepts like HTML elements manipulation, event handling, and inline event attributes. It showcases how JavaScript can be used to create dynamic and interactive web content, Hence, let's delve into this example to understand these key concepts better.

Bulb On Off function in Js
Bulb On Off function in Js

The Bulb toggle Example: HTML Essentials

Before diving into the JavaScript part of the bulb example, let's first ensure a solid understanding of the HTML structure involved.

CrΓ©er des animations sans code Javascript
CrΓ©er des animations sans code Javascript

In this example, an HTML tag is used to display a bulb image. Two different images are used to represent the on and off states of the bulb. The tag's attribute is used to uniquely identify the bulb element, enabling JavaScript to interact with it.

JavaScript Event Handling

Power Of Light v0.2 | Html CSS & Vanilla Javascript Mousemove Effects
Power Of Light v0.2 | Html CSS & Vanilla Javascript Mousemove Effects

In the JavaScript part of the example, the 'click' event of the bulb image is handled. Every time the image is clicked, the 'click' event is triggered, which then runs the associated JavaScript function. This is a classic demonstration of event-driven programming, a fundamental concept in JavaScript.

The 'on' attribute within the tag is used to specify the JavaScript function to be run when the event occurs. The attribute is crucial here, as it allows the function to specifically target the bulb image.

JavaScript Image Swapping

JavaScript Basics | Chapter 1 πŸ“– | Part 1
JavaScript Basics | Chapter 1 πŸ“– | Part 1

The function specified in the 'on' attribute handles the event by swapping the images (bulb on and bulb off). It does this by alternating the 'src' attribute of the tag between two different image paths, thus creating a toggle effect.

This simple task might seem inconsequential, but it forms the foundation for more complex JavaScript functionalities, such as manipulating CSS styles, adjusting text content, or even managing entire DOM structures.

Expanding the Bulb Example: Style Control

Javascript Fundamental
Javascript Fundamental

While the basic bulb toggle teaches the fundamentals of JavaScript, the example can be extended to demonstrate more advanced concepts, like external JavaScript files and direct manipulation of CSS styles by JavaScript.

By linking an external JavaScript file, we can separate our JavaScript code from the HTML content, promoting a cleaner and more maintainable code structure.

JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step
an image of a computer screen with text on it
an image of a computer screen with text on it
JavaScript Basics | Chapter 1 πŸ“– | Part 2
JavaScript Basics | Chapter 1 πŸ“– | Part 2
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
an info sheet with different types of web pages
an info sheet with different types of web pages
Java script string method
Java script string method
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
Javascript Project Ideas πŸ‘
Javascript Project Ideas πŸ‘
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Fundamental
JavaScript Fundamental
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
Click Events in JavaScript
Click Events in JavaScript
Best JavaScript Projects for Beginners to Practice
Best JavaScript Projects for Beginners to Practice
Javascript tutorials
Javascript tutorials
JavaScript Methods: Add Functionality to Your Objects πŸš€
JavaScript Methods: Add Functionality to Your Objects πŸš€

Manipulating CSS Styles

JavaScript has the ability to directly manipulate CSS styles. Using this capability, we can change the style of the bulb image with each click, such as alternating its 'display' property between 'block' and 'none', or modifying its 'width' and 'height'.

This not only adds to the interactivity of the page but also showcases how JavaScript can be used to control and modify both the content and styling of a web page.

Understanding the bulb example on W3Schools is not just about grasping the code but also about learning and practicing crucial JavaScript skills. As you progress in your learning journey, remember that mastering the basics, from understanding HTML elements to handling events, is key to unlocking JavaScript's full potential. So, keep practicing and expanding your skills to become a proficient JavaScript developer.