JavaScript - Simple Star Rating

Submitted by: 
Language: 
Visitors have accessed this post 668 times.

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 a 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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Simple Star Rating</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <div>
  21. <h3>Rating:</h3>
  22. <span id="1" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)" ></span>
  23. <span id="2" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  24. <span id="3" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  25. <span id="4" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  26. <span id="5" style="font-size:45px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  27. </div>
  28. <br />
  29. <div class="form-group">
  30. <h3>Review:</h3>
  31. <textarea id="review" class="form-control" style="resize:none; height:100px;"></textarea>
  32. </div>
  33. <center><button class="btn btn-success" onclick="result()">SUBMIT</button></center>
  34. <div id="result"></div>
  35. </div>
  36. </div>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </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 help you to what you are looking for. For more updates and tutorials just kindly visit this site. 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.