Remove onClick event set in code?

Hi, I’m working with buttons event listeners in code, and I need to add onClick events to button, and change them later. So I need to know how to remove old onClick listeners, or owerride it with new onClick event listener, or to at least know if there is any onClick listener registered.

Currently all calls to onClick add new event handler, and don’t remove old event handlers, so if I were to do something like this:

$w("#button").onClick( (event) => { console.log("A"); }) 
$w("#button").onClick( (event) => { console.log("B"); }) 

Clicking on button #button would result with both “A” and “B” printed to console.
How can I override or remove old onClick listeners, so I’d only get “B” printed out?

you could disable it by putting // in front of the line of code you are not using atm.

1 Like

LOL, I was excepting to get a few stupid answers, but this is hilarious. Sites I’m building have over 10000 lines of JS, so yeah, I’m well aware of comments.
If I had asked how to compute 5+7, I guess someone would have proposed to just write 12 instead :slight_smile:
The situation I’m dealing with involves dynamically sorting custom repeater filled from code, so I have trouble rewiring event logic once repeater items change their indexes - that’s why I have to deal with invalidating event listeners.


@littlebeargamesdoo ohh sorry bud, yea way above my newbie head then.

1 Like

Hi @littlebeargamesdoo !

I am having trouble with understanding what is the need in the same event listener twice.
You can use it once and in the promise to set a condition or the desired logic in order to achieve your goal.

$w("#button").onClick( (event) => { 
    if ('your_use_case') {
    else {

Let me know if this is what you’ve been missing.
If not, please elaborate more on your use case so we can find a more suitable solution for your needs.

In the future please be more tolerant to other fellow users.
WixCode is part of a growing community that we try to raise and as one we’re trying to guide and embrace in oppose to discourage the new and less-experienced.


1 Like

Every! programming language (and language library) has the ability to invalidate the set listener, there are thousands of reasons why something like this would be needed, that’s why everyone supports it (except Wix it seems).
It’s like asking why would I use recursive calls when I can just put all function argument on stack and have it executed linearly from function stack, LOL?

In my case (as I already mentioned in a comment to previous reply) - I have a repeater which is filled from code, and I have buttons on each repeater items, and those buttons do different things depending on order, and I have to set onClick listener to those buttons. But some of those buttons change the order of item in the repeater (move items up or down in the list) and once item is in different place in list, it needs to have different onClick listener, since action triggered by button in each item depend on it’s order in repeater.

BTW. I’m somewhat puzzled with your motivation - is seems as you don’t know the answer to my question, so instead you’re trying to make me believe that there is no need to answer it in the first place. That would be messed up…


First, as the logo in the top left corner may suggest, WixCode is a BETA product (and not a programming language). As such, some features that are obvious to advanced programmers may be missing. I urge you to submit a feature request here .

Second, to your case, not sure if you’re familiar with the function .onItemReady() that allows you to create new events for newly generated items of a repeater.
In order to arrange them (the items) as you describe (according to a button +1/-1) I’d build two arrays and use the ‘Index’ argument that is part of the method to determine which item called the function in the first place and according to the . onClick() event to move it one spot up/down. Obviously you might have some ‘edge-cases’ to consider and solve.

Third, let me solve the puzzle for you.
As part of the WixCode team, when a user presents me with a problem my job is to understand the use case and try to provide the best guidance accordingly. At some occasions it may require a few follow-up questions from my side. Apologies for the inconvenience.



Thanks for clarification.
I use onItemReady(), and I did try the obvious approach you suggested.
It doesn’t work, so let me clarify a few things about this function (which should be in Wix docs, but isn’t):
Assigning new data to repeater doesn’t create new items if there is already enough of them created (similar to how Android’s view-lists work), so onItemReady() doesn’t get called, it’s called only for new items, not when old items get new data.

So since I get the old items once I assign new data, those old object have old onClick() listeners.

1 Like

There must be someone smart enough to hack around this restriction from Wix.

Looking again through the docs I can assure you that it is written - you might have missed it (although it’s the first paragraph - I’d suggest you go over it again) :

About moving items up and down a repeater as a list, well, if that’s your goal - It works for me:


1 Like

I also have a working version of moving up and down - but handling click listeners while moving items up and down is an issue.
Randomly set unique text on each item to differentiate between items, them add button at the bottom that adds new item, and then try adding to your example a button that removes current item, and you’ll see the issue. Clicking on raw to delete current raw will also delete raw at the previous location, since assigning to = updatedDataAfterRawWasRemoved will add new listeners to old raw object with old listeners.

Hi Guys, is it solved after all? I need to register/unregister event listeners in an Model / View / Presenter architecture I am building in Wix. Due to its complexity I cannot use the if/else method suggested above. This should be a low hanging fruit to solve, but it holds us back and limiting Wix Code to simple things. Please prioritize if not yet fixed.

Yea, this is pretty stupid. I expected a new onClick() to replace the old onClick() as well but as you mentioned, it actually appends to the old handler, super odd!

You could try use Andrii’s wrapper above or try a workaround. In my case, I wanted a button that will delete a repeated item from a repeater. So the onClick() functionality goes something like: get repeater data, splice out array index x, set repeater data. The problem is index i is coded upon the very first onItemReady() so if you start with 3 items in the repeater, button 1, 2 and 3 will have x passed in as 0, 1 and 2. Say a user clicks the first delete button, it will splice array index 0 - which is the correct functionality for now. The repeater now only has elements 2 and 3. If the user clicks delete button 2 (which is now the first button), the delete button will splice array index 1 (which is the second repeated item) not index 0 because it was coded with x = 1. What I had to do was create an array with [0, 1, 2] which maps the delete button to the correct repeated item I want to delete. So when the user clicks the first delete button, I update the array to [-, 0, 1] and if the user then clicks the second button (which is now the first button), it will splice array index 0 instead of 1.

Old post from 2018 being closed.