Partwise Get PartwiseBuy
Forms

Rating

Stars to click, built on radio buttons so they work in forms and with a keyboard. Plus stars that only show a score.

Stars to click

How was your stay?
HTML
<fieldset class="pw-rating" data-pw-rating data-labels="Poor,Fair,Good,Very good,Excellent" id="demo-rating">
  <legend>How was your stay?</legend>
  <span class="pw-rating-stars">
    <label><input type="radio" name="stay" value="1"></label>
    <label><input type="radio" name="stay" value="2"></label>
    <label><input type="radio" name="stay" value="3"></label>
    <label><input type="radio" name="stay" value="4" checked></label>
    <label><input type="radio" name="stay" value="5"></label>
  </span>
  <span class="pw-rating-text"></span>
</fieldset>

Stars that show a score

CSS only. Set the score in --value, from 0 to 5. Parts of a star work too.

4.5 from 128 reviews
HTML
<span class="pw-stars" style="--value: 4.5" role="img" aria-label="4.5 out of 5 stars"></span>
<span>4.5 from 128 reviews</span>

Options

Set them as attributes on the element.

AttributeDefaultWhat it does
data-labelsA word for each star, shown next to the stars.
data-cleartruefalse: a second click on the chosen star does not clear it.
data-emptyText when no star is chosen.

Classes

ClassWhat it does
pw-rating--lgLarger stars.
pw-starsScore only, not clickable.

Events

Listen with addEventListener. Details are in event.detail.

EventWhen
pw:changeWith value, 0 to 5.

From your script

var r = Partwise.get('#demo-rating', 'rating');
r.value; r.value = 5;

Keyboard

KeyAction
Left RightOne star less or more
TabInto and out of the stars

Good to know

  • The star colour is --pw-rating.
  • Add required to the first radio button to make a rating necessary.
  • For another number of stars, write more or fewer radio buttons.

Files

Everything is in dist/partwise.css and dist/partwise.js. To load only this component, use src/css/00-base.css and src/js/00-core.js plus: css/23-inputs.css, js/23-inputs.js.