I am making a grid on the screen. The grid is composed of different expressions. I want all of them to have a Gray background, which turns into a White background upon a mouse click, each expression separately. Instead of the whole thing I only show it here for a row of 3 elements to simplify the example. This works:
DynamicModule[{col1 = Gray, col2 = Gray, col3 = Gray},
Row[{
EventHandler[Dynamic@Style[x, Background -> col1],
{"MouseClicked" :> (col1 =
col1 /. {Gray -> White, White -> Gray})}](*End eventhandler*),
EventHandler[Dynamic@Style[x, Background -> col2],
{"MouseClicked" :> (col2 =
col2 /. {Gray -> White, White -> Gray})}](*End eventhandler*),
EventHandler[Dynamic@Style[x, Background -> col3],
{"MouseClicked" :> (col3 =
col3 /. {Gray -> White, White -> Gray})}](*End eventhandler*)
}]
]
However, as soon as I try to automate the same code, i.e., instead of writing each element of the row separately, to use the Table statement it does not work.
This is my trial of the automation that does not work:
DynamicModule[{col1 = Gray, col2 = Gray, col3 = Gray},
Row[Table[
EventHandler[
Dynamic@Style[x, Background -> ToExpression["col" <> ToString[i]]],
{"MouseClicked" :> (Background -> ToExpression["col" <> ToString[i]] =
Background -> ToExpression["col" <> ToString[i]] /. {Gray -> Green,
Green -> Gray})}](*End eventhandler*),
{i, 1, 3}]]
]
I would like to understand what is wrong. Is it possible to wrap EventHandler by a Table statement?





Togglerand friends thanEventHandler, as István might be suggesting by his linked content. That having been said, I think Mr.Wizard points to a better method for tracking a variable number of values. – John Fultz Oct 23 '12 at 2:46