Creating a Progress Bar using JavaScript

JavaScript is a powerful language that allows web developers to create dynamic and interactive web pages. One of the most useful features that JavaScript can be used for is creating a progress bar.

In this tutorial, we will walk you through the process of creating a progress bar using JavaScript. A progress bar is a graphical representation of the progress of a task, and can be used to give the user feedback on how long a task will take to complete. We will use JavaScript to update the progress bar as the task progresses, giving the user a visual indication of how far along the task is.

HTML Structure

We start by creating the basic HTML structure for our progress bar. This includes a container for the progress bar and a button to trigger the animation.

<h3>Progress Bar Using JavaScript</h3>

<!-- The button to update the progress bar -->
<button id="update-button">Update Progress</button>

<!-- The progress bar element -->
<div id="progress-bar"></div>

The button has an ID of "update-button" and the progress bar has an ID of "progress-bar". We'll use these IDs to reference the elements in our JavaScript code.

CSS Styling

Next, we define the styling for the progress bar container and the fill element that will represent the actual progress.

#progress-bar {
    width: 100%;
    height: 15px;
    background-color: #f1f1f1;
    border-radius: 20px;
}

#progress-bar-fill {
    height: 100%;
    background-color: #4CAF50;
    border-radius: 20px;
    width: 0%;
    transition: width 0.1s ease;
}

The progress bar container has a light gray background, while the fill element uses green color and starts with 0% width. The transition property creates a smooth animation effect.

JavaScript Functionality

Now we implement the JavaScript logic to animate the progress bar from 0% to 100%.

// Get the progress bar element and the update button 
const progressBar = document.getElementById("progress-bar");
const updateButton = document.getElementById("update-button");

// Update the progress bar with a new progress value
function updateProgressBar(progress) {
    // Create a new element to represent the progress bar fill
    progressBar.innerHTML = "<div id='progress-bar-fill'></div>";

    // Get the progress bar fill element from the HTML
    const progressBarFill = document.getElementById("progress-bar-fill");

    // Set the width of the progress bar fill based on the progress value
    progressBarFill.style.width = `${progress}%`;
}

// Update the progress bar when the button is clicked
updateButton.addEventListener("click", function () {
    let progress = 0; // Set the initial progress value to 0

    // Update the progress bar every 100 milliseconds until it reaches 100%
    const intervalId = setInterval(function () {
        progress += 1; // Increment the progress value
        updateProgressBar(progress); // Update the progress bar with the new progress value

        // Stop the interval when the progress reaches 100%
        if (progress === 100) {
            clearInterval(intervalId); 
        }
    }, 100);
});

The `updateProgressBar` function creates the fill element and sets its width based on the progress value. The event listener uses `setInterval` to increment the progress by 1% every 100 milliseconds until it reaches 100%.

Complete Working Example

<!DOCTYPE html>
<html>
<head>
    <title>Progress Bar Using JavaScript</title>
    <style>
        /* Styling for the progress bar */
        #progress-bar {
            width: 100%;
            height: 15px;
            background-color: #f1f1f1;
            border-radius: 20px;
            margin: 20px 0;
        }

        /* Styling for the progress bar fill */
        #progress-bar-fill {
            height: 100%;
            background-color: #4CAF50;
            border-radius: 20px;
            width: 0%;
            transition: width 0.1s ease;
        }
        
        button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #008CBA;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h3>Progress Bar Using JavaScript</h3>

    <!-- The button to update the progress bar -->
    <button id="update-button">Start Progress</button>

    <!-- The progress bar element -->
    <div id="progress-bar"></div>

    <script>
        // Get the progress bar element and the update button
        const progressBar = document.getElementById("progress-bar");
        const updateButton = document.getElementById("update-button");

        // Update the progress bar with a new progress value
        function updateProgressBar(progress) {
            // Create a new element to represent the progress bar fill
            progressBar.innerHTML = "<div id='progress-bar-fill'></div>";

            // Get the progress bar fill element from the HTML
            const progressBarFill = document.getElementById("progress-bar-fill");

            // Set the width of the progress bar fill based on the progress value
            progressBarFill.style.width = `${progress}%`;
        }

        // Update the progress bar when the button is clicked
        updateButton.addEventListener("click", function () {
            let progress = 0; // Set the initial progress value to 0

            // Disable button during animation
            updateButton.disabled = true;
            updateButton.textContent = "In Progress...";

            // Update the progress bar every 50 milliseconds until it reaches 100%
            const intervalId = setInterval(function () {
                progress += 1; // Increment the progress value
                updateProgressBar(progress); // Update the progress bar

                // Stop the interval when the progress reaches 100%
                if (progress === 100) {
                    clearInterval(intervalId);
                    updateButton.disabled = false;
                    updateButton.textContent = "Start Progress";
                }
            }, 50);
        });
    </script>

</body>
</html>

How It Works

When you click the "Start Progress" button, the progress bar animates from 0% to 100% over 5 seconds (100 steps × 50ms each). The button is disabled during the animation to prevent multiple simultaneous progress bars. Once complete, the button is re-enabled for another animation.

Key Features

  • Smooth Animation: CSS transitions create fluid movement
  • User Feedback: Button text changes during progress
  • Customizable Speed: Adjust interval timing and increment values
  • Responsive Design: Progress bar scales with container width

Conclusion

Creating a progress bar with JavaScript involves combining HTML structure, CSS styling, and JavaScript functionality. This implementation provides a smooth, animated progress bar that can be easily customized for different use cases in web applications.

Updated on: 2026-03-15T23:19:01+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements