Getting Started with EJ2 JavaScript Rating

21 Nov 20266 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Rating and demonstrate the basic usage of the Rating control in a JavaScript application.

Dependencies

The list of dependencies required to use the Rating component in your application is given below:

|-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-popups

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Rating control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and Scripts

Add Syncfusion® Rating CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-basescripts are required dependency packages for the Rating component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/rating/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js

Adding Rating component to the application

Add the Rating element and initiate the JavaScript (ES5) Rating control in the index.html by using following code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js"></script>
  <title>JavaScript (ES5)</title>
  <!-- JavaScript (ES5) Rating's dependency style -->
  <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/rating/index.css" rel="stylesheet" />

  <!-- JavaScript (ES5) Rating's dependency global script -->
  <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
  <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>

  <!-- JavaScript (ES5) Rating's control global script -->
  <script src="https://cdn.syncfusion.com/ej2/35.2.12/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>

  <!--style reference from app-->
  <link href="index.css" rel="stylesheet" />
</head>

<body>
  <!--Element to render the Rating control-->
  <div class='wrap' style="margin: 50px auto; text-align: center;">
    <input id="rating" />
  </div>
  <script>
    ej.base.enableRipple(true);

    // initialize Rating control
    var rating = new ej.inputs.Rating({});

    // Render initialized Rating.
    rating.appendTo('#rating');
  </script>
  <script>
    var ele = document.getElementById('container');
    if (ele) {
      ele.style.visibility = "visible";
    }   
  </script>
</body>

</html>
#container {
    visibility: hidden;
    padding-left: 5%;
    width: 100%;
  }
  #loader {
    color: #008cff;
    font-family: 'Helvetica Neue','calibiri';
    font-size: 14px;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
  }

  .wrap {
    margin: 50px auto;
    text-align: center;
  }

Value

You can set the rating value by using the value property.

ej.base.enableRipple(true);

// Initialize the Rating control.
var rating = new ej.inputs.Rating({value:3.0});

// Render initialized Rating.
rating.appendTo('#rating');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Rating</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="TypeScript Rating Component">
    <meta name="author" content="Syncfusion">
    <link href="styles.css" rel="stylesheet">
    <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/rating/index.css" rel="stylesheet" />
    
    
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
    
    <div id="container">
        <div class="wrap">
            <input id="rating">
        </div>
    </div>

<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  font-family: 'Helvetica Neue','calibiri';
  font-size: 14px;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}

.wrap {
  margin: 50px auto;
  text-align: center;
}