🌟5 Star Rating component: A Step-by-Step Guide 🌟
Tags: html, css, javascript, webdev
Published: 2024-02-29 08:20:33

Feedback forms are essential for businesses to gather valuable insights from their users. Adding star ratings to feedback forms can make the process more engaging and intuitive for users. In this guide, we'll walk through the process of creating a feedback form with star ratings using HTML, CSS, and JavaScript.
[Demo here](https://codepen.io/vikas2426/pen/JjVjKEM)
## Step 1: HTML Structure ☠

![html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4wnalxnxs2vbepf3lgd.gif)
First, let's define the structure of our feedback form in HTML. We'll create a simple form with star ratings and a textarea for additional comments.

```html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Feedback Form</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <div class="feedback-form">
        <h2>Feedback Form</h2>
        <form id="feedbackForm">
            <div class="rating">
                <!-- Notice that the stars are in reverse order -->

                <input type="radio" id="star5" name="rating" value="5">
                <label for="star5">&#9733;</label>
                <input type="radio" id="star4" name="rating" value="4">
                <label for="star4">&#9733;</label>
                <input type="radio" id="star3" name="rating" value="3">
                <label for="star3">&#9733;</label>
                <input type="radio" id="star2" name="rating" value="2">
                <label for="star2">&#9733;</label>
                <input type="radio" id="star1" name="rating" value="1">
                <label for="star1">&#9733;</label>
            </div>
            <div class="comment">
                <label for="comment">Tell us more:</label><br>
                <textarea id="comment" name="comment"></textarea>
            </div>
            <button type="submit" class="submit-btn">Submit</button>
        </form>
    </div>

    <script src="./script.js"></script>
</body>

</html>
```

## Step 2: CSS Styling 💅

![css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4aer1z4uev19pghczc1.gif)
Next, let's add some CSS styles to make our feedback form visually appealing. We'll style the star ratings and the textarea.

```css
body {
  font-family: Arial, sans-serif;
}

.feedback-form {
  max-width: 400px;
  margin: 0 auto;
}

.rating {
  margin-bottom: 20px;
  display: flex;
  flex-direction: row-reverse; /* this is the magic */
  justify-content: flex-end;
}

.rating input {
  display: none;
}

.rating label {
  font-size: 24px;
  cursor: pointer;
}

.rating label:hover,
.rating label:hover ~ label { /* reason why the stars are in reverse order in the html */
  color: orange;
}

.rating input:checked ~ label {
  color: orange;
}

.comment {
  margin-bottom: 20px;
}

.comment textarea {
  width: 100%;
  height: 100px;
  resize: none;
}

.submit-btn {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  border-radius: 5px;
}

```

> _**Note:** "~" is the [Subsequent sibling combinator](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Combinators#subsequent-sibling_combinator) a CSS selector, which only allows us to select the subsequent siblings in the html order, there is no selector or combinator which lets us select the preceding siblings and this is why we wrote the stars in reverse order in the HTML file._

Step 3: JavaScript Functionality 🧩


![js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cf4w6gpzkwjeq1me11b.png)
Finally, let's add JavaScript code to handle the form submission.

```js
document.getElementById("feedbackForm").addEventListener("submit", function (event) {
    event.preventDefault();
    var rating = document.querySelector('input[name="rating"]:checked').value;
    var comment = document.getElementById("comment").value;

    // Here you can handle the submission, for now just logging the values
    console.log("Rating: ", rating);
    console.log("Comment: ", comment);

    // You can send this data to the server using AJAX or other methods
    // For simplicity, I'm just logging it here
});
```

Conclusion 🎉
In this tutorial, we've learned how to create a feedback form with star ratings using HTML, CSS, and JavaScript. By following these steps, you can easily implement a user-friendly feedback mechanism on your website or web application, allowing users to provide valuable input in a visually appealing way.

Feel free to customize and expand upon this template to fit the specific requirements of your project. Happy coding! 😊
[Demo here](https://codepen.io/vikas2426/pen/JjVjKEM)
## Connect with me on my:
[LinkedIn](https://www.linkedin.com/in/vikas-choubey/)
[GitHub](https://github.com/Vikas2426/)
[Twitter](https://twitter.com/itsvikas7/)