• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Simple Star Rating using JavaScript Tutorial

abo88

Haha Hero
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
124
Likes
138
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "continer-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    target

    =

    "_blank"

    >

    Sourcecodester</

    a

    >
  12. </

    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. function

    result(

    )

    {
  3. if

    (

    count !=

    0

    )

    {
  4. document.getElementById

    (

    'result'

    )

    .innerHTML

    =
  5. "<h4>Rating: <label class='text-primary'>"

    +

    count +

    "</label></h4>"
  6. +

    "<h4>Review</h4>"
  7. +

    "<p>"

    +

    document.getElementById

    (

    "review"

    )

    .value

    +

    "</p>"

    ;
  8. }

    else

    {

  9. }
  10. }

  11. function

    startRating(

    item)

    {
  12. count=

    item.id

    [

    0

    ]

    ;
  13. sessionStorage.star

    =

    count;
  14. for

    (

    var

    i=

    0

    ;

    i<

    5

    ;

    i++

    )

    {
  15. if

    (

    i <

    count)

    {
  16. document.getElementById

    (

    (

    i+

    1

    )

    )

    .style

    .color

    =

    "yellow"

    ;
  17. }
  18. else

    {
  19. document.getElementById

    (

    (

    i+

    1

    )

    )

    .style

    .color

    =

    "black"

    ;
  20. }
  21. }
  22. }

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,193

322,229

322,238

Top