Creating a Simple Star Rating using JavaScript Tutorial

Language

In this tutorial we will create a Simple Star Rating using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has the capabilities of Object-Oriented.

So Let's do the coding...

Getting started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display the form. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. <link rel="stylesheet" type="text/css" href="fontawesome/css/all.css" />
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="continer-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com" target="_blank">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Star Rating</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div>
  20. <h3>Rating:</h3>
  21. <span id="1" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)" ></span>
  22. <span id="2" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  23. <span id="3" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  24. <span id="4" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  25. <span id="5" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  26. </div>
  27. <br />
  28. <div class="form-group">
  29. <h3>Review:</h3>
  30. <textarea id="review" class="form-control" style="resize:none; height:100px;"></textarea>
  31. </div>
  32. <center><button class="btn btn-success" onclick="result()">SUBMIT</button></center>
  33. <div id="result"></div>
  34. </div>
  35. </div>
  36. <script src="js/script.js"></script>
  37. </body>
  38. </html>

Creating the Script

This code contains the script of the application. This code will display the review and rating of the users. To do this just copy write the code as shown below inside the text editor and save it as script.js.

  1. var count = 0;
  2.  
  3. function result(){
  4. if(count != 0){
  5. document.getElementById('result').innerHTML =
  6. "<h4>Rating: <label class='text-primary'>" + count + "</label></h4>"
  7. + "<h4>Review</h4>"
  8. + "<p>"+document.getElementById("review").value+"</p>";
  9. }else{
  10.  
  11. }
  12. }
  13.  
  14. function startRating(item){
  15. count=item.id[0];
  16. sessionStorage.star = count;
  17. for(var i=0;i<5;i++){
  18. if(i < count){
  19. document.getElementById((i+1)).style.color="yellow";
  20. }
  21. else{
  22. document.getElementById((i+1)).style.color="black";
  23. }
  24. }
  25. }

There you have it we successfully created a Simple Star Rating using JavaScript. I hope that this very simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Demo

Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment