0 votes
by (230 points)

Using SugarCube 2 and just starting to get my feet wet. I've come across an issue I assume can be done more easily.

Doing a character creation screen of sorts, so I'm using <<replace>> to reflect choices on the fly. However, I started writing the variable in different places which I assumed I could simply use the same id to replace it again.

Perhaps an example of my script will better explain things.

<<link "Confirm">><<replace "#nickname">><<print $nickname>><</replace>><</link>>

So I then put this in different places:

You are <span id="nickname"><<print $nickname>></span>
...
People call you <span id="nickname"><<print $nickname>></span> because ...

But clicking the link only updates the first instance. How would I replace multiple instances of a span id or do I need to make more than one?

1 Answer

+2 votes
by (159k points)
edited by
 
Best answer

The ID given to each HTML element of the current page must be unique, however you can assign the same CSS class name to multiple elements of the current page.

<<set $nickname to "Aaaaa">>

You are <span class="nickname"><<print $nickname>></span>
...
People call you <span class="nickname"><<print $nickname>></span> because ...

<<link "Confirm">>
    <<set $nickname to "Bbbbb">>
    <<replace ".nickname">><<print $nickname>><</replace>>
<</link>>


note: You can also use Custom Style markup like the following to create the same span element as the above example. You can also use Naked Variable markup to print the value of basic variables.

<<set $nickname to "Aaaaa">>

You are @@.nickname;$nickname@@
...
People call you @@.nickname;$nickname@@ because ...

<<link "Confirm">>
    <<set $nickname to "Bbbbb">>
    <<replace ".nickname">>$nickname<</replace>>
<</link>>


 

by (230 points)
I see, thanks!

Your 2nd example using Custom Style looks simpler and compact, I think I'll look into this option further.

Thank you very much for your help.
...