Page 2 of 2

Re: How to remove onClick handler in Javascript

Posted: 02 March 2021, 22:05
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?

Re: How to remove onClick handler in Javascript

Posted: 02 March 2021, 22:15
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 ?

Re: How to remove onClick handler in Javascript

Posted: 02 March 2021, 22:26
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.

Re: How to remove onClick handler in Javascript

Posted: 03 March 2021, 11:06
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.

Re: How to remove onClick handler in Javascript

Posted: 03 March 2021, 11:27
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.

Re: How to remove onClick handler in Javascript

Posted: 03 March 2021, 17:34
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.

Re: How to remove onClick handler in Javascript

Posted: 25 March 2021, 22:34
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.

Re: How to remove onClick handler in Javascript

Posted: 26 March 2021, 03:09
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.