How to remove onClick handler in Javascript

Game development with Board Game Arena Studio
User avatar
Hornir91
Posts: 38
Joined: 15 October 2018, 06:39

Re: How to remove onClick handler in Javascript

Post by Hornir91 »

I'm thinking about changing the gamestate after click on a card, because I saw that changing state, removes click events from the divs. Is that right?
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: How to remove onClick handler in Javascript

Post by Tisaac »

Hornir91 wrote: 02 March 2021, 22:05 I'm thinking about changing the gamestate after click on a card, because I saw that changing state, removes click events from the divs. Is that right?
No. Why would you think that ?
User avatar
Hornir91
Posts: 38
Joined: 15 October 2018, 06:39

Re: How to remove onClick handler in Javascript

Post by Hornir91 »

Because when I'm clicking on a player token, it moves and it's no longer clickable after changing the player. If changing the gamestate doesn't remove connected events, making additional gamestate to clear all selectables before going to another player makes sense. Should I always try to clean-up everything before entering activeplayer state?

I'm sorry for stupid questions, but I'm creating my first game here, which is a complex one and don't know everything.
User avatar
bidulpik
Posts: 79
Joined: 17 November 2011, 09:02

Re: How to remove onClick handler in Javascript

Post by bidulpik »

Hi,
have you tried the CSS solution ?

https://developer.mozilla.org/fr/docs/W ... ter-events

or

to put the attribute disabled=disabled after clicking an object ?

I've got functions I lauch on convinient states in the onEnteringState
where I activate or not buttons, clicks ...


hope this help

best regards.
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: How to remove onClick handler in Javascript

Post by Tisaac »

Hornir91 wrote: 02 March 2021, 22:26 Because when I'm clicking on a player token, it moves and it's no longer clickable after changing the player. If changing the gamestate doesn't remove connected events, making additional gamestate to clear all selectables before going to another player makes sense. Should I always try to clean-up everything before entering activeplayer state?

I'm sorry for stupid questions, but I'm creating my first game here, which is a complex one and don't know everything.
There are several approaches to do that, but clearing stuff when leaving state sound a good idea in general.
User avatar
Hornir91
Posts: 38
Joined: 15 October 2018, 06:39

Re: How to remove onClick handler in Javascript

Post by Hornir91 »

bidulpik wrote: 03 March 2021, 11:06 Hi,
have you tried the CSS solution ?

https://developer.mozilla.org/fr/docs/W ... ter-events

or

to put the attribute disabled=disabled after clicking an object ?

I've got functions I lauch on convinient states in the onEnteringState
where I activate or not buttons, clicks ...


hope this help

best regards.
I'll try it today. Also I was thinking about another approach and will also try it. Thanks in general.
User avatar
Hornir91
Posts: 38
Joined: 15 October 2018, 06:39

Re: How to remove onClick handler in Javascript

Post by Hornir91 »

I know now where was the problem.

When I was using

Code: Select all

this.clickHandlers.push(dojo.query(element).connect('onclick', this, handler))
with

Code: Select all

dojo.forEach(this.clickHandlers, dojo.disconnect);
It didn't worked, because instead of removing only the event listener, dojo was removing a whole element.

I tried the newer solution with:

Code: Select all

this.clickHandlers.push(dojo.query(element).on('click', this, handler))
combined with:

Code: Select all

dojo.forEach(this.clickHandlers, function(entry) {
entry.remove();
})
And it worked perfectly.

So if anybody is dealing with the same problem, there is a solution.
Last edited by Hornir91 on 28 March 2021, 21:37, edited 1 time in total.
User avatar
tsaunat
Posts: 30
Joined: 28 April 2020, 05:10

Re: How to remove onClick handler in Javascript

Post by tsaunat »

I constantly frustrated that it's super easy to bind events based on queries, it is not as easy to unbind the handlers.
Post Reply

Return to “Developers”