JavaScript - Simple User Review Rating

In this tutorial we will create a Simple User Review Rating using JavaScript. This code will display the user review when the user submit the input form. The code use onclick() function to launch a specific function that apply a animated rating star that created using sessionStorage that temporary store a data and for() loop in order to loop the star value. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. 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">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 User Review Rating</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-4">
  18.                         <div class="form-group">
  19.                                 <label>User:</label>
  20.                                 <input type="text" id="user" class="form-control" />
  21.                         </div>
  22.                         <div>
  23.                                 <h3>Rating:</h3>
  24.                                 <span id="1" style="font-size:30px; cursor:pointer;"  class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)" ></span>
  25.                                 <span id="2"  style="font-size:30px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  26.                                 <span id="3"  style="font-size:30px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  27.                                 <span id="4"  style="font-size:30px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  28.                                 <span id="5"  style="font-size:30px; cursor:pointer;" class="fa fa-star" onmouseover="startRating(this)" startRating="starmark(this)"></span>
  29.                         </div>
  30.                         <br />
  31.                         <div class="form-group">
  32.                                 <h3>Review:</h3>
  33.                                 <textarea id="review" class="form-control" style="resize:none; height:100px;"></textarea>
  34.                         </div>
  35.                         <center><button class="btn btn-success" onclick="submitRate()">Submit</button></center>
  36.                 </div>
  37.                 <div class="col-md-6">
  38.                         <div id="result"></div>
  39.                 </div>
  40.         </div>
  41. <script src="js/script.js"></script>   
  42. </body>
  43. </html>

Creating the Script

This code contains the script of the application. This code will display user review when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var rate = 0;
  2.  
  3. function submitRate(){
  4.         var user=document.getElementById('user').value;
  5.         var review=document.getElementById('review').value;
  6.         if(rate != 0 && user !="" && review !=""){
  7.                 var html=
  8.                 "<h4>User: <label class='text-primary'>" + user + "</label></h4>"
  9.                 +"<h4>Rating: <label class='text-primary'>" + rate + "</label></h4>"
  10.                 +"<h4>Review</h4>"
  11.                 +"<p>"+review+"</p>"
  12.                 +"<hr style='border-top:1px solid #000;'/>";
  13.                 document.getElementById('result').innerHTML+=html;
  14.                
  15.                 document.getElementById('user').value="";
  16.                 document.getElementById('review').value="";
  17.         }
  18. }
  19.  
  20. function startRating(item){
  21.         rate=item.id[0];
  22.         sessionStorage.star = rate;
  23.         for(var i=0;i<5;i++){
  24.                 if(i<rate){
  25.                         document.getElementById((i+1)).style.color="yellow";
  26.                 }
  27.                 else{
  28.                         document.getElementById((i+1)).style.color="black";
  29.                 }
  30.         }
  31. }
There you have it we successfully created a Simple User Review Rating using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

This is cool

hello

Add new comment