Action Button Toggles On and Off

I came across a CL example slide that toggled a lightbulb image on and off with an action button and wanted to use the idea in an activity, but now I can find it, does anyone know that I am talking about?

Set a number in your graph to the pressCount
number(`n`): button.pressCount
Then next to one of the dimensions, or one of the center coordinates, add:

You’re basically setting a restriction that will make a value undefined if it equals 0 instead of one, thus not displaying the image.

1 Like

Okay, I think I see what you’ve done there. I had to search and found a similar thread where SqrtOfPi used this, which is mostly the same(?) to understand what mod was. There seems to be a lot of variation to achieve the same result.
hidden: numericValue("\mod(${p},2)")=0

I ended up finding the example:

which has this:
label: when counter = 1 “On” otherwise “Off”
style: when counter = 1 otherwise buttonStyles.default
counter = simpleFunction("\operatorname{mod}\left(x,2\right)").evaluateAt(this.pressCount)

Agh, my head hurts. It looks like it all needs to be tied into an image/graph, but if I just wanted it to toggle text in the Note above the Action Buttons, would I do something like this? The mod would make it so each button toggles the text on or off rather than having the text pop up when the button is hit once?

hidden: numericValue("\mod(${p1},2)")=0

hidden: numericValue("\mod(${p2},2)")=0

when p1 = 1 “Great Job! Move on to the next slide.”
when p2 = 1 “Hint”

The mod function takes a number and gets the remainder when divided by a quotient. (mod 2) can only result in remainders of 0 or 1, even and odd numbers respectively. Thus, the toggle (e.g. the button clicked 5 times is odd, so 1 controls the various toggles).

counter is doing what I put in the graph, though unneccesarily complicated. You don’t need two buttons (harder to make two buttons work actually). The mod calculation toggles the button label, button color, and the two different images.

For your note:

when p1 = 1 "Great Job! Move on to the next slide."
otherwise "Hint"

Set a number in your graph to the pressCount
number(n): button.pressCount
Then next to one of the dimensions, or one of the center coordinates, add:

Is it necessary to have a graph component for this? I just want a button to toggle a display between two equations for students… I suppose I could have a graph component but make it hidden the whole time…

I got this working but it only switches once, then it stays on one displayed note. I’d like it to continue to switch between note1a and note1b. I made the graph display to try to debug, showing y=a{mod(a,2)=1} and its a flat line only defined for odd numbers but it continues to increase with button pushes…

Got it! Edited in my solution. Defined a variable b=mod(a,2) where number('a'): button.pressCount and switch the displayed notes based on b, not a, using hidden:

1 Like

It’s not necessary to have a graph component. It’s just a little cleaner doing calculations in graphs than in the CL where you need to use numericValue( ) or simpleFunction( ).evaluateAt( ), but it’s not always more efficient.
So rather than adding a graph, in note1 (whatever you call it):

hidden: n=1

In note2:

hidden: note1.script.n=0

Perfect, thanks! I added a second copy of my slide with your solution in the link above.

I wasn’t aware numericValue was a function.

The other way of performing calculations is to use simpleFunction( ).evaluateAt( ), which for non-terminating decimals is more stable than numericValue. You can either enter exactly as you would in numericValue and .evaluateAt( ) any number:


…or you can create a function (more useful when the same operations are carried out with different values) with variables and .evaluateAt( ) your intended input:


Note: simpleFunction defaults to in terms of x. More than one variable, or a variable other than x must be defined. Thus, the "a" parameter in toggle.