Skip to content

JavaScript Five-Star Rating

The jSuites.rating plugin is designed to be easily integrated into various web development environments. Its compatibility with frameworks like React, Angular, and VueJS and its mobile-friendly design makes it versatile for adding rating functionality to web applications.

  • React, Angular, and VueJS compatible.
  • Mobile-friendly interface.
  • Easy to integrate with several events.
  • Available as both a JavaScript plugin and a web component.
MethodDescription
getValue();Get the current value
setValue(number)Set a new value
@param {int} newValue - Set a new value
PropertyDescription
number: numberHow many stars to be rendered. Default: 5
value: numberInitial value. Default: null
tooltip: arrayLegend for the stars. Default: [ 'Very bad', 'Bad', 'Average', 'Good', 'Very good' ]
MethodDescription
onchangeTrigger a method when value is changed.
onchange(element: DOMElement, value: number) => void

Here's a quick example of a five-star rating with a specified value and tooltip:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<script src="https://jsuites.net/v6/jsuites.webcomponents.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<jsuites-rating value="4" tooltip="Ugly, Bad, Average, Good, Outstanding"></jsuites-rating>

<div id='console'></div>

<script>
document.querySelector('jsuites-rating').addEventListener('onchange', function(e) {
    document.getElementById('console').innerHTML = 'New value: ' + this.value;
});
</script>
</html>

Basic example of a five-star rating embed in a web application.

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<span id='rating1'></span>

<script>
let instance = jSuites.rating(document.getElementById('rating1'), {
    value: 4,
    tooltip: ['Ugly', 'Bad', 'Average', 'Good', 'Outstanding'],
    onchange: function () {
        console.log('changed');
    }
});
</script>
</html>

Customize the number of stars for the rating plugin with events.

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<span id='rating2'></span>

<p><input type="button" value="setValue(2)" id="button" /></p>

<script>
let instance = jSuites.rating(document.getElementById('rating2'), {
    number: 10,
    tooltip: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
    onchange: function () {
        console.log('changed');
    }
});
    
document.getElementById('button').addEventListener('click', () => {
    instance.setValue(2);
}) 
</script>
</html>