Page 1 of 1

[solved] Dojo question : select/deselect dices

Posted: 01 May 2020, 13:50
by Axel584
Hi,
I've got 5 dices :

Code: Select all

<div id="dice_1" class="dice" style="left:10px; top :10px;"></div>
<div id="dice_2" class="dice" style="left:120px; top :10px;"></div>
<div id="dice_3" class="dice" style="left:230px; top :10px;"></div>
<div id="dice_4" class="dice" style="left:340px; top :10px;"></div>
<div id="dice_5" class="dice dice_selected" style="left:450px; top :10px;"></div>
And I would like to add or remove the class "dice_selected" when I click on it.
In the setup part, I've put :

Code: Select all

dojo.connect( $('#dice'), 'onclick', this, 'onSelectDice' );
and I add a "onSelectDice" function :

Code: Select all

onSelectDice: function(evt) {
			console.log("onSelectDice ");
		},
It works great.

But I would like to "switch" (add if the class doesn't exist and remove if the element already have the class "dice_selected").

How can I do this ? How can I know on which of the five dices the function has been called ?

Thank you,

Axel

Re: Dojo question : select/deselect dices

Posted: 01 May 2020, 15:00
by fafa-fr
Axel584 wrote: Hi,
I've got 5 dices
...
And I would like to add or remove the class "dice_selected" when I click on it.
...
But I would like to "switch" (add if the class doesn't exist and remove if the element already have the class "dice_selected").
How can I do this ?
dojo allows to toggle() a class in addition to add(), remove(), replace(), ...
Axel584 wrote: How can I know on which of the five dices the function has been called ?
The evt argument of your onSelectDice has a currentTarget property that you can use to know that. See at the end of Reversi tutorial

Re: Dojo question : select/deselect dices

Posted: 01 May 2020, 17:19
by Axel584
Thank you very much, it's work great !

in my setup :

Code: Select all

dojo.query( '.dice' ).connect( 'onclick', this, 'onSelectDice' );
and in the function :

Code: Select all

onSelectDice: function(evt) {
			dojo.stopEvent( evt );
			dojo.toggleClass(evt.currentTarget.id,"dice_selected");
		},
8-)

Re: [solved] Dojo question : select/deselect dices

Posted: 01 May 2020, 17:26
by vincentt
hi,

You can take a look on diceforge code, we used connects on each dice and when you click on it we removed before all the other "dice selected" :)

Vincent