How to give the player the option to type a sentence and after submitting to go to a passage

Twine Version:Twine 2.3.15
Story Format: Sugarcube 2.34.1

Hello!
I want to give the player the option to type an answer and depending on which option they choose to be taken to another passage but I’ve been struggling and cannot make it work…
This is my code:

What do you think?
<span id="output">
People should’ve continued to do that because
<<textarea $response "Type your answer here">>
<<button "Submit">>
<</button>>
</span>

People shouldn't have done that because
<<textarea $response "Type your answer here">>
<<button "Submit">>
<</button>>
</span>

Thank you!

First of all, there is an error in your coding. The $response variable should be in quotation marks.

Next, you will need to tell the Submit button to take the player to another passage, like this:


What do you think?
<span id="output">
People should’ve continued to do that because
<<textarea "$response" "Type your answer here">>
<<button "Submit">><<goto "destination">><</button>>
</span>

People shouldn't have done that because
<<textarea "$response" "Type your answer here">>
<<button "Submit">><<goto "destination">><</button>>
</span>

Then you will have to create a page titled destination, and make that page actually do something. For example: <<print $response>>

You could also have more than one destination page, or more than one $response variable.

To elaborate on what P.B. said at the end there, having both <<textarea>> macros set the same variable could cause problems, since then that variable will get set to the text in whichever one of those <<textarea>>s was last edited, regardless of which button you pushed.

Thus, I’d change the code to something like this:

What do you think?

People should’ve continued to do that because:
<<textarea "_should" "Type your answer here">>
<<button "Submit" "Next Passage Name">>
	<<set $response = _should>>
<</button>>

People shouldn't have done that because:
<<textarea "_not" "Type your answer here">>
<<button "Submit" "Next Passage Name">>
	<<set $response = _not>>
<</button>>

By setting two different temporary variables, and then assigning the value from the temporary variable related to the button the user clicked to the $response story variable, that would prevent the wrong text from being submitted.

Also note that you can combine the <<button>> and <<goto>> macros, simply by setting the second parameter of the <<button>> macro to the name of the passage you want to go to. (So, you’ll need to change “Next Passage Name” to the name of the passage you want to send the player to when they click those buttons.)

You may also want to label the response, so you can tell which button was clicked. For example, you could do something like this instead:

	<<set $response = "Should: " + _should>>
...
	<<set $response = "Should not: " + _not>>

Now you’ll be able to easily tell which <<textarea>> the value of $response comes from.

Note: I also removed the <span> stuff from the code, since it didn’t appear to be implemented correctly. If you want both of those answer areas to be affected by some CSS, then, instead of using an ID, you should use a class on those spans. IDs are supposed to be unique on the page, but multiple HTML elements can all have the same class at the same time. If you change your CSS from “#output” (i.e. ID = "output) to “.output” (class = “output”), then you could do this instead:

What do you think?

<span class="output">\
	People should’ve continued to do that because:
	<<textarea "_should" "Type your answer here">>
	<<button "Submit" "Next Passage Name">>
		<<set $response = "Should: " + _should>>
	<</button>>\
</span>

<span class="output">\
	People shouldn't have done that because:
	<<textarea "_not" "Type your answer here">>
	<<button "Submit" "Next Passage Name">>
		<<set $response = "Should not: " + _not>>
	<</button>>\
</span>

The backslashes (\) at the ends of lines there are just to prevent unnecessary line breaks, while keeping the code nice and readable as well.

Hope that helps! :slight_smile:

2 Likes

Thank you both for the help!!