Star Rating using HTML CSS and JavaScript (original) (raw)

Last Updated : 02 Jun, 2025

Star rating is a way to give a rating to the content to show its quality or performance in different fields. This article will demonstrate how to create a star rating project using JavaScript.

Project Preview:

Screenshot-from-2023-07-26-11-16-31

Prerequisites

**Step-by-Step Guide to Building a Star Rating System

**Example: In this example, we will create the star rating with the above approach.

index.html `

Star Rating

JavaScript Star Rating


Rating is: 0/5

style.css

/* style.css */

index.js

// script.js

// To access the stars let stars = document.getElementsByClassName("star"); let output = document.getElementById("output");

// Funtion to update rating function gfg(n) { remove(); for (let i = 0; i < n; i++) { if (n == 1) cls = "one"; else if (n == 2) cls = "two"; else if (n == 3) cls = "three"; else if (n == 4) cls = "four"; else if (n == 5) cls = "five"; stars[i].className = "star " + cls; } output.innerText = "Rating is: " + n + "/5"; } // To remove the pre-applied styling function remove() { let i = 0; while (i < 5) { stars[i].className = "star"; i++; } }

`

**Output:

starrrrrr

Star Rating using HTML CSS and JavaScript