# Count and display button clicks

**URL:** <https://forum.wixstudio.com/t/count-and-display-button-clicks/10030>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [March 1, 2021, 1:13pm UTC](https://forum.wixstudio.com/t/count-and-display-button-clicks/10030 "2021-03-01T13:13:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![info80859](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@info80859](https://forum.wixstudio.com/u/info80859)\
**Post date:** [March 1, 2021, 1:13pm UTC](https://forum.wixstudio.com/t/count-and-display-button-clicks/10030/1 "2021-03-01T13:13:31Z")

</div>

Hey everyone! I’m trying to implement a button and a text, which displays the total clicks on the button. This is my code so far:

import wixData **from** ‘wix-data’ ;

export function button1\_click(event) {  
wixData.query( “Klicks” ).find().then((result)=\>{

**let** answersitem = result.items[0]; //item with values  
**let** id = answersitem.\_id; // this will be used for  
//writing data back

```
    answersitem.result++; // increment field with yes by 1 

```

// write updated value back to collection  
wixData.update( “Klicks” , answersitem).then(()=\>{  
// refresh dataset to show new value  
//(this should be done in promise,  
//after updating collection)  
$w( “#dataset1” ).refresh();  
});  
})  
}

My Collection is called “Klicks”, the corresponding line in the database “totalKlicks”, the button “button1” and the text, which should display the total clicks: “result”.

I took this piece of code from another forum question but always recieve the answer “$w(…).refresh is not a function” when running.

Does anyone have an idea how to solve this?

Thanks already!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 1, 2021, 7:44pm UTC](https://forum.wixstudio.com/t/count-and-display-button-clicks/10030/2 "2021-03-01T19:44:14Z")

</div>

[https://russian-dima.wixsite.com/meinewebsite/blank-26](https://russian-dima.wixsite.com/meinewebsite/blank-26)

---

<div class="post-metadata">

**Author:** ![info80859](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@info80859](https://forum.wixstudio.com/u/info80859)\
**Post date:** [March 1, 2021, 7:48pm UTC](https://forum.wixstudio.com/t/count-and-display-button-clicks/10030/3 "2021-03-01T19:48:28Z")

</div>

Thank You, but I need a counter which counts all clicks globally 🙂

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [March 1, 2021, 10:52pm UTC](https://forum.wixstudio.com/t/count-and-display-button-clicks/10030/4 "2021-03-01T22:52:34Z")

</div>

@info80859

```javascript
import wixLocation from 'wix-location';
 
var clickCounter //<--- global variable??? isn't it?
 
$w.onReady(function () {
    clickCounter = 0
});
 
export function button1_click(event) {
    clickCounter=clickCounter+1
 
    if (clickCounter>0) {$w('#vector1').show()}
    if (clickCounter>1) {$w('#vector2').show()}
    if (clickCounter>2) {$w('#vector3').show()}
    if (clickCounter>3) {hide_allSTARS(), wixLocation.to("https://www.google.de")}
    $w("#TXT").text = clickCounter.toString()
}
 
function hide_allSTARS (parameter) {$w('#vector1').hide()}

```

What exactly do you mean, when you say…

> I need a counter which counts all clicks globally 🙂  
> There could be a lot of interpretations for this sentence.

Perhaps you use the search of this forum, i am sure you will find a lot of other posts, related to the given topic…

> **[Count Button Clicks](https://forum.wixstudio.com/velo/forum/coding-with-velo/count-button-clicks)**
>
> I have an E-Book that I am giving away for free. I have attached the e-book to a button on one of my web pages. I would like to know how many times the e-book is downloaded. Is there some way to record the number of times the button is clicked.
