Hello,
I recently switched to wix code and I would like to know if there is a way to create a click counter connected to a button.
For exemple: a question with a "Yes" and "No" as answer buttons...
I would like to collect the amount of clicks for each answer and display it on my website.
Please let me know if there is a way for me to do that.
Thank you
Hello Can someone help me how to create a download counter using a button and a testo that shows how many times it was downloaded? Thank you
hi, how can i add a button using wix code to increment a text field by 1 and at the same time onclick on this button to be linked to an external url
Hello Mikhail,
I copy pasted ur code into my website on my button as onClick event and it still doesn't work. Nothing is being added to the collection at all. Any ideas why?
hello mikhail,
how can i make a dataset save the random number obtained from this code to a db
export function button1_click(event) { $w("#text3").text = (Math.floor((Math.random() * 20) + 1)).toString(); }
Hi, i guess i know what happens here
Field names are case-sensitive. In code you used "linkfield", but from content manager view it looks like your field is named "linkField". That means that after code is executed, it addes likes to feild "linkfield"
At the same time, on page, your text field (i believe) is connected to text to show number of likes
In code you first assign text element to number of likes which you get from collection (and you see this number) and then refresh dataset, which results in taking variable from current item from database, with field key "linkItem"
It's empty and your text becomes empty
Ask more questions if needed
Mikhail, are you still there? So this codes works such that it increases the like counter field. However I am still stuck on how to get the number to stay on screen.
Doing:
$w('#text71').text = answersitem.likefield.toString();
Displays the number of likes for 1 second, then the number disappears. How do I get this number to stay?
oooh, so after I made my changes it seems to work.
https://puu.sh/zX1oo/ae59ac736b.png
https://puu.sh/zX1pE/0600819372.png
That is my new code. And it is using [likefield] instead of likefield on the datebase.
The issue I am having now is how to get the number of likes to stay on screen. The number of likes only gets displayed for 1 second after I click the button, then it disappears.
Another question I have is, if I have multiple like buttons to connect them to the correct row on the database, would I just change this line of code:
let answersitem = result.items[x];
I would just change the x value to the corresponding row, right?
Hi! First of all, i can't understand what does this mean - ...eq('title',$w('#likeButton')).find....
for eq operator you need to pass key field as first paramenter and smth to compare with as 2nd parameter - should be some value
But, youre passing whole component as 2nd parameter - $w('#likeButton')
Hey Mikhail! I have looked through the other posts, and still can't figure out why it's not working for me. My collection is called Artists and there is only field, likeField. I'm using #text71 as my output to display the number of likes for the user. Any ideas as to why this isn't working?
Thanks Mikhail! You got the point, case-sensitive is key of the problem. But not only "title", "Noc" also is a problem. The final code is following:
wixData.query("NewCollectionName").eq('title',clicktitle).find().then((result) => {
let answersitem = result.items[0];
let id = answersitem._id;
answersitem.noc++; // instead of "answeritem.Noc //
Thanks again! : >
And second idea -
https://www.screencast.com/t/2WTRQiIqH
I see that in query code you used field name instead of actual field key
'Title' is name, but field key is 'title' (it's just guess, i don't see on screens your real field key)
And such things are commonly case-sensitive, so try to chyange query to this:
wixData.query("NewCollectionName").eq('title',clicktitle).find().then((result) => {
You syncronized collections, right? I mean live collection has data?
Actually, it shows nothing. But you're right, the content of answersitem seems nothing inside which means my query maybe wrong. I change my code as following to check the answeritem, and $w('#text9').text doesn't make any change, as well as no error msg. Comes with me database picture for your reference. Thanks! Anything I get wrongly?
export function text2_click(event, $w) {
let clicktitle = $w('#text2').text;
wixData.query("NewCollectionName").eq('Title',clicktitle).find().then((result) => {
let answersitem = result.items[0];
let id = answersitem._id;
answersitem.Noc++;
$w('#text9').text = answersitem.Title;
// wixData.update("NewCollectionName",answersitem).then(()=>{
// $w('#dataset2').refresh();
// });
});
Hi!
So what are the syptoms, what actually happens when you try to run this code?
I believe you should check content of answersitem before updating db - this is first thing to check. Dos it really change?
Hi Mikhail! I'm try to count how many click on the data and follow your code as following. Could you tell me what I did wrong?
My database called "NewCollectionName"
the counter field called "NOC" // number of click
import wixData from 'wix-data';
export function text2_click(event, $w) {
wixData.query("NewCollectionName").eq('Title',$w('#text2').text).find().then((result) => {
let answersitem = result.items[0];
let id = answersitem._id;
answersitem.Noc++;
wixData.update("NewCollectionName",answersitem).then(()=>{
$w('#dataset2').refresh();
});
})
}
Hey, I've already implemented code like this and I know it works because I've tested it myself and saw that the counter increments. The problem is that I'm not noticing any change when other users click on it, because I'm getting subscribers through it.
Is there any reason why the code might not work for other users? Is it because they might have javascripts deactivated?
Hi Mikhail! Thanks for that. I did fix that yesterday, that's unfortunately not the problem though. For good measure I started over and redid the whole thing just now, all of the event handlers are accurate.
The ID problem is still there.
So good time of the day.
I have new ideas how to explain it)
Problem in your case is that there is no connection between event handler in properties panel (https://www.screencast.com/t/fBL9XajTGC)
And function which should be executed when action (click in this case) happen
Best way to fix this is to delete everything in onclick event, create new one and add your code to this new function. I made a video:
https://www.screencast.com/t/3rklPguu
I believe I may have fixed what you are referring to!
Or are you referring to "export function InstructionalManualtext_click() {" not having "...click(event) {"?
Because that prompts another error, and I read in another forum to delete that, which has worked for my other codes.
Either way the problem remains.
this function which you renamed doesn't have any connection with the text click event.
Again, please read article about creating event handlers - i can't explain you better than it's explained there.