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

JavaScript - Simple User Review Rating

chrøma

Epic Fail Analyst
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
173
Likes
197
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. <body

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "continer-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    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 User Review Rating</

    h3

    >
  17. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  18. <div

    class

    =

    "col-md-4"

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    User:</

    label

    >
  21. <input

    type

    =

    "text"

    id

    =

    "user"

    class

    =

    "form-control"

    /

    >
  22. </

    div

    >
  23. <div

    >
  24. <h3

    >

    Rating:</

    h3

    >
  25. <span

    id

    =

    "1"

    style

    =

    "font-size:30px; cursor:pointer;"

    class

    =

    "fa fa-star"

    onmouseover

    =

    "startRating(this)"

    startRating=

    "starmark(this)"

    ></

    span

    >
  26. <span

    id

    =

    "2"

    style

    =

    "font-size:30px; cursor:pointer;"

    class

    =

    "fa fa-star"

    onmouseover

    =

    "startRating(this)"

    startRating=

    "starmark(this)"

    ></

    span

    >
  27. <span

    id

    =

    "3"

    style

    =

    "font-size:30px; cursor:pointer;"

    class

    =

    "fa fa-star"

    onmouseover

    =

    "startRating(this)"

    startRating=

    "starmark(this)"

    ></

    span

    >
  28. <span

    id

    =

    "4"

    style

    =

    "font-size:30px; cursor:pointer;"

    class

    =

    "fa fa-star"

    onmouseover

    =

    "startRating(this)"

    startRating=

    "starmark(this)"

    ></

    span

    >
  29. <span

    id

    =

    "5"

    style

    =

    "font-size:30px; cursor:pointer;"

    class

    =

    "fa fa-star"

    onmouseover

    =

    "startRating(this)"

    startRating=

    "starmark(this)"

    ></

    span

    >
  30. </

    div

    >
  31. <br

    /

    >
  32. <div

    class

    =

    "form-group"

    >
  33. <h3

    >

    Review:</

    h3

    >
  34. <textarea

    id

    =

    "review"

    class

    =

    "form-control"

    style

    =

    "resize:none; height:100px;"

    ></

    textarea

    >
  35. </

    div

    >
  36. <center

    ><button

    class

    =

    "btn btn-success"

    onclick

    =

    "submitRate()"

    >

    Submit</

    button

    ></

    center

    >
  37. </

    div

    >
  38. <div

    class

    =

    "col-md-6"

    >
  39. <div

    id

    =

    "result"

    ></

    div

    >
  40. </

    div

    >
  41. </

    div

    >
  42. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  43. </

    body

    >
  44. </

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

    submitRate(

    )

    {
  3. var

    user=

    document.getElementById

    (

    'user'

    )

    .value

    ;
  4. var

    review=

    document.getElementById

    (

    'review'

    )

    .value

    ;
  5. if

    (

    rate !=

    0

    &&

    user !=

    ""

    &&

    review !=

    ""

    )

    {
  6. var

    html=
  7. "<h4>User: <label class='text-primary'>"

    +

    user +

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

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

    +

    rate +

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

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

    "<p>"

    +

    review+

    "</p>"
  11. +

    "<hr style='border-top:1px solid #000;'/>"

    ;
  12. document.getElementById

    (

    'result'

    )

    .innerHTML

    +=

    html;

  13. document.getElementById

    (

    'user'

    )

    .value

    =

    ""

    ;
  14. document.getElementById

    (

    'review'

    )

    .value

    =

    ""

    ;
  15. }
  16. }

  17. function

    startRating(

    item)

    {
  18. rate=

    item.id

    [

    0

    ]

    ;
  19. sessionStorage.star

    =

    rate;
  20. for

    (

    var

    i=

    0

    ;

    i<

    5

    ;

    i++

    )

    {
  21. if

    (

    i<

    rate)

    {
  22. document.getElementById

    (

    (

    i+

    1

    )

    )

    .style

    .color

    =

    "yellow"

    ;
  23. }
  24. else

    {
  25. document.getElementById

    (

    (

    i+

    1

    )

    )

    .style

    .color

    =

    "black"

    ;
  26. }
  27. }
  28. }

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!


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

452,496

336,529

336,537

Top