When a cell has WholeCellGroupOpener set to True, it becomes a clickable button that toggles its cell group open and closed.

How can I make the cell change color when I press down on it so I know I have clicked the right thing? This is the behavior you would expect to see from most buttons.

Here's what it looks like - the issue is that you can't tell when I press the mouse down:

enter image description here

Here's the code I tried so far in the stylesheet:

 CellEventActions->{"MouseDown" :> (SelectionMove[
      InputNotebook[], All, ButtonCell]; SetOptions[
       InputNotebook[]], Background -> LightGreen]), "MouseUp" :> (SelectionMove[
      InputNotebook[], All, ButtonCell]; SetOptions[
       InputNotebook[]], Background -> White]; FrontEndExecute[
  • $\begingroup$ What you are asking for is not the normal behavior for openers even outside of Mathematica. $\endgroup$
    – m_goldberg
    Aug 28, 2015 at 14:44
  • $\begingroup$ I guess so, but I think the triangle on the left is ugly so I hide it so this sort of visual feedback would be great $\endgroup$
    – M.R.
    Aug 28, 2015 at 14:46
  • $\begingroup$ @m_goldberg does the gif I added to the post help you see what I mean? $\endgroup$
    – M.R.
    Aug 28, 2015 at 22:29

1 Answer 1


You can use CellEventActions by adding it to the Stylesheet or hard-coding it for each cell. Here is a cell that changes when clicked.

DynamicModule[{bgd = LightRed}, 
        TextCell["Click within this cell", "Output", 
            Background -> Dynamic[bgd], 
            CellEventActions -> {"MouseClicked" :> (bgd = LightGreen)}

Rather than having it change once you can have it set a variable that indicates the CellID or something which determines whether or not the cell should be highlighted, such as the following two cells which toggle back and forth when clicked (disclaimer, this uses a lot of Dynamic stuff and may choke the frontend):

DynamicModule[{selected = 3}, 
        TextCell["Click within this cell", "Output", 
            Background -> Dynamic[If[selected === 1, Yellow, Gray]], 
            CellEventActions -> {"MouseClicked" :> (selected = 1)}], 
        TextCell["Click within this cell", "Output", 
            Background -> Dynamic[If[selected === 2, Yellow, Gray]],
            CellEventActions -> {"MouseClicked" :> (selected = 2)}]

UPDATE (to embed in stylesheet): The following let's this work in a stylesheet, although MouseDown and MouseUp behave differently when clicking between cells and this will cause some unexpected behaviour.

    CellEventActions -> {
        "MouseDown" :> SetOptions[EvaluationCell[], Background -> GrayLevel[0.5]], 
        "MouseUp" :> Quiet[
            SetOptions[EvaluationCell[], Background -> GrayLevel[1]];
            SelectionMove[EvaluationCell[], All, Cell];
        PassEventsDown -> True
    (*uncomment the following if you want Deployed like WholeCellGroupOpener*)
        Deployed -> True

UPDATE 2: Probably what you were looking for

        "MouseDown" :> 
            With[{cell = EvaluationCell[]}, 
                SetOptions[cell, Background -> GrayLevel[0.5]]; 
                RunScheduledTask[SetOptions[cell, Background -> GrayLevel[1]], {0.2, 1}]], 
        "MouseUp" :> RemoveScheduledTasks[ScheduledTasks[]],
         PassEventsDown -> True},
    WholeCellGroupOpener -> True
  • $\begingroup$ I don't want an action not on mouse clicked, but only when pushing down... also this doesn't work in stylesheets. $\endgroup$
    – M.R.
    Aug 28, 2015 at 23:07
  • 1
    $\begingroup$ You can use the event handler MouseDown rather than MouseClicked if you wish, there is also MouseUp, MouseEntered and MouseExited if you want to get fancy :). It can be modified to work in stylesheets using the CellID option and CurrentValue, but it would take some finesse. $\endgroup$
    – Kyle Keane
    Aug 28, 2015 at 23:12
  • $\begingroup$ Oh yeah, and MouseMoved... $\endgroup$
    – Kyle Keane
    Aug 28, 2015 at 23:34
  • 1
    $\begingroup$ Append PassEventsDown->True to the CellEventActions and it should work $\endgroup$
    – Kyle Keane
    Aug 31, 2015 at 1:44
  • 1
    $\begingroup$ Also I'm not sure if ButtonCell is a valid selector for SelectionMove, I guess it is safer just to use Cell $\endgroup$
    – Kyle Keane
    Aug 31, 2015 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.