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.
Yes
Colin, are you Peter Glick? (security question).
Event handlers that are created by Property and Events panel are separate functions and not included in the main code headed by onReady statements, sorry if I misled. You might have to delete and re-create the handler if it has been moved. Use console.log statements to check if the handler is working, and if your values are calculated and converted to displayable formats.
Input elements (boxes) can be connected to a database, or not, whatever is needed.
If all else fails, if I can see what your collection looks like, maybe I can re-create your code here...
I still don´t succede with making it work. If I don´t connect the input element to the database nothing happends and when I do that the display element shows no change or triples up. Very strange so please help me.
For starters, your $w("#dataset2").onReady(() function should be inside your page load $w.onReady( function() so that it is not executed every time the ratingsInput1 button is clicked. More console.log statements may help also.