How do I assign (text) user input to a variable and read said input in the game?

I've tried several versions of

input type="text" id="getit"

squiffy.set("name", jQuery("#getit").val());

And have tried to read it with {name} but it doesn't work. I keep getting true for val()
And when I change val() to text() nothing shows up?
Is there any way to do this?


Here's the trick.

[[init]]:

<textarea id="text_first"></textarea>

[[Enter your hero's name]](next)

[[next]]:

    squiffy.set("heroName", jQuery("#text_first").val());
    
Welcome to your adventure, {heroName}

You gotta do it in two sections.


Thank you so much! It works!


Wait, heck it's a bit incomplete. When I circle back to check if the user made a mistake when typing in the name, it keeps saying the {heroName} is what was first entered, instead of the new name the user enters the nth time.
Is it possible to overwrite?

this is the code by the way
That is your hero name right?
[[Yes it is]] (contd) [[No wait, I made a mistek]] (init)


The code looks up the first input field named text_first. So if that's still on the screen, it will read the value from there instead of the one printed later.

One way to prevent this problem would be removing the text field's ID after it's been found; by changing the javascript line to:

    squiffy.set("heroName", jQuery("#text_first").removeAttr('id').val());

You might also like to make the field readonly, so that the player doesn't see an input field in the previous section and think they can still change it:
One way to prevent this problem would be removing the text field's ID after it's been found; by changing the javascript line to:

    squiffy.set("heroName", jQuery("#text_first").removeAttr('id').prop('readonly', true).val());

I think (can't verify this) that I use @clear statements all over the place so it automatically clears it for me. Sorry. I kinda remember facing this same thing and finding out that clear solved it.


Log in to post a reply.

Support

Forums