Can a button set a value to a variable?

Should be easy I think, but I’m still learning, slowly. I didn’t want to ask for something that seems so simple, but I’ve been trying for hours now and so I gave up, I’ll ask. How can this be done?
Basically I want to give the value (the value is a tiddler’s name) to a variable when only when I click a button.

Yes you can change the value of a variable in the action of the button, but this would be useless, because the scope of this variable would be limited to your action!

You have to store a flag somewhere and use that flag (or directly your target value), for instance in a temporary tiddler ($:/temp/) or in a field of such a tiddler.

1 Like

Ah understood. This was an important detail that I was missing, thanks! Instead just to set the value with the button what should I do?

<$button actions="""<$action-setfield $tiddler="$:/temp/store" myfield="myvalue" """>set value</$button>

and elsewhere

<$let myvar = {{$:/temp/store!!myfield}}> do your stuff here </$let>
1 Like

@jypre What is the lifespan of the temporary tiddler?

If someone saves the tiddlywiki file, does that save the temporary tiddler? Is “temporary tiddler” just a concept of being created in $:/temp/?

Or does everything in $:/temp/ get deleted when you first open a tiddlywiki? Or tiddlers created in $:/temp/ automatically disappear at some point?

When you save the TiddlyWiki file, it uses a saver “template” named $:/core/save/all, which defines a macro called saveTiddlerFilter() that contains a filter to control which tiddlers are saved to the file.

The filter includes syntax: -[prefix[$:/temp/]], which prevents all tiddlers with the $:/temp/ prefix from being saved (note the leading “-” on the filter syntax). Thus, when you save and reload your TiddlyWiki, all $:/temp/ tiddlers are gone without your having to do anything.

2 Likes

@EricShulman Awesome, thanks! I am fairly new to TW and I am learning. Glad to know that temp is truly temp and that it is cleaned up between saves.

And consider other names for saved eg prefix $:/state/ and consider using the Qualify macro for unique to the current tiddler position.

The thing is the difference between tiddlers and their fields vs the temporary “in memory variables”. We need to use triggers such as buttons to commit a variable to the tiddlers/tiddler store, the things in a tiddlers store are absolute, specific.

Variables names and their values on the other hand can be reused, are contextual and effectively “only exist as things are rendered”.

  • Alternative views welcome.

Ignore what I wrote just a few minutes ago, I’ve overlooked one syntax error I made earlier, and I found about it only after writing this. Anyway.
Thank you very much for your help jpre!

Old message:

I’m sure this is the solution, but I can’t get it to work. :confused: Would you be kind enough to explain to me what you mean by elswhere (or anyway how this code works)?
I’m sorry if I’m being a burden