Fix “Cannot read property ‘toFixed’ of undefined” for Yet Another Starts Rating Plugin

Table of Contents

Issue

When using Yet Another Starts Rating WordPress Plugin on mobile, users can’t vote for article, there is a console error message of “Cannot read property ‘toFixed’ of undefined”.

Cause

The Yet Another Starts Rating plugin using EventListener of “mousemove” to setup the “currentrating” value, which does not work on mobile.

Solution

  1. Find rater-js.js file under plugin folder.
  2. find onStarClick function, and add these codes before callback method
//for mobile, redo it for mobile touchover if (typeof currentRating === 'undefined'){ if (disabled === true || isRating === true) { return; } var xCoor = e.offsetX; var width = elem.offsetWidth; var percent = xCoor / width * 100; if (percent < 101) { if (step === 1) { currentRating = Math.ceil(percent / 100 * stars); } else { var rat = percent / 100 * stars; for (var i = 0;; i += step) { if (i >= rat) { currentRating = i; break; } } } elem.querySelector('.star-value').style.width = currentRating / stars * 100 + '%'; if (showToolTip) { var toolTip = ratingText.replace('{rating}', currentRating); toolTip = toolTip.replace('{maxRating}', stars); elem.setAttribute('data-title', toolTip); } if (typeof onHover === 'function') { onHover(currentRating, rating); } } }

This can be improved by adding a touchmove listener, but current solution is good for use.

This websiteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:「Fix “Cannot read property ‘toFixed’ of undefined” for Yet Another Starts Rating Plugin」

268
0 0 268

Post a reply

Log inYou can only comment after that.
Share this page
Back to top