I can´t figure out why my rating triples up each time you click on a star, from 52 to 55 and from 55 to 58 etc? Here is my code and I have made the fields heterosexRating, heterosexNumber and heterosexTotal which I have connected to the dataset. Someone wrote that you shall not connect the input element to the dataset (only the display element) but when I don´t do that nothing happends when you click on a star. Very frustrating. I guess that the tripled acounting depens on that I have connected the input element or what is your guess? 😉
export function ratingsInput1_change(event) {
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
$w("#dataset2").onReady(() => {
// get the current item from the dataset
const currentItem = $w("#dataset2").getCurrentItem();
// get the current average rating, number of ratings, and
//total ratings for the current dataset item
const average = currentItem.heterosexRating;
const count = currentItem.heterosexNumber;
const total = currentItem.heterosexTotal;
// get the new rating from the ratings input
const newRating = $w('#ratingsInput1').value;
// calculate the new average rating based on the current
//average and count
const newAverageLong = (total + newRating) / (count +1);
// Round the average rating to 1 decimal point
const newAverageShort = Number.parseFloat(newAverageLong).toFixed(1);
// set the dataset fields to the new average, total
// ratings, and number of ratings
$w('#dataset2').setFieldValues({
'heterosexRating': newAverageShort,
'heterosexTotal': total + newRating,
'heterosexNumber': (count + 1)
});
// save the dataset fields to the collection
$w('#dataset2').save()
.catch((err) => {
console.log('could not save new rating');
});
});
// Add your code for this event here:
}
![](https://static.wixstatic.com/media/a27d24_6fcebc6ddeea4e97afa7fd470d04fe7f~mv2.jpg/v1/fill/w_692,h_174,al_c,q_80,enc_auto/a27d24_6fcebc6ddeea4e97afa7fd470d04fe7f~mv2.jpg)
Your code should start with $w.onReady( function() { to ensure the page loads before accessing any page elements. Then your $w("#dataset2").onReady(() => { should run to ensure your dataset is ready to access your collection elements. No need for more page or dataset onReady statements.
$w.onReady( function() {
$w("#dataset2").onReady(() => {
.
.
});
});
For further explanation, see https://www.wix.com/velo/forum/tips-tutorials-examples/w-onready-a-guide-for-the-perplexed
Hope this helps.