0 votes
by (2.2k points)
edited by

Using Adventures/snowman and I'm trying to assign a class name to a passage link and I don't even know if that's possible.

Okay, so I'm using a user input in passage A and I want passage B to understand what was input, which will either be "somename" or whatever the user inputs. But I noticed that I had to set the name to "somename" before passage B or the "somename" won't show up properly in the passage if declared in the passage itself (go figure). 

So I figured the best way to do this would be shoving an onclick or click to the passage link but I didn't entirely know how to do that...

My next thought was to put a classname on the passage and use the click on the passage, which hasn't worked at all because the classname isn't working. 

[[[B]]
]{.namesubmit}
<% 
$(.namesubmit).click(if(!character.name == "" || null)
{character.name = 'somename'}); 
%>

This is what my code looks like thus far. It returns errors which is probably right considering I don't entirely know what I'm doing. Good thing is that I know the (!character.name == "" || null) does work! So I seem to be picking up on stuff.

So basically, I want to set character.name to 'somename' in the event that the user doesn't input a name themselves when the passage link is clicked so that way it shows up on passage B. Any ideas?

2 Answers

0 votes
by (63.1k points)
selected by
 
Best answer

I feel like you've asked this before, possibly twice. Deja vu. Here's a fresh solution in case the last one(s) didn't work. Tested in snowman, should work in adventures:

Put this in your JavaScript:

window.getName = window.getName || function (nextPsg) {
	var name = $('input#name').val();
	name = name.trim();
	
	if (!name) {
		name = 'somename';
	}
	
	character.name = name;
	
	if (nextPsg && typeof nextPsg === 'string') {
		story.show(nextPsg);
	}
	
};

...Where 'somename' is the default name. 

Put this in the passage:

<input id='name' />

<a href='javascript:void(0)' onclick='getName("next")'>Confirm</a>

...Where "next" is the name of the passage you want to go to next.

Then, to display the name:

<%= character.name %>

So now let's deal with your code:

[[[B]]
]{.namesubmit} <- what is this? it might be something in adventures or some weird part of snowman, i'm really not sure.  i doubt it does what you think it does though
<% 
$(.namesubmit).click(if(!character.name == "" || null)
{character.name = 'somename'}); <- there's a number of things wrong here
%>

assuming your other code is working, it should look like this:

<span class='namesubmit'>[[B]]</span>

<%
$('span.namesubmit a').click( function () { // selectors are strings, also, you chould wrap this code in a function, since the click() method accepts functions as arguments, not random code
    if (!character.name) { // don't need all that other stuff, also, i think the (!) was an accident?
        character.name = 'somename';
    }
});
%>

 

by (2.2k points)
Haha, I have asked about the name thing before but I resolved that, but thanks for the code anyway. I was only asking in this question if I could assign a class name to a link in order to change a variable when it is clicked instead of after getting to the second passage, sorry if what I was asking wasn't clear enough in that regard.

Lol, what is this? I ask that as well because I was just looking at the snowman documentation for that. It was the adding an id to something I believe.

For the 'span.namesubmit a' is a the name of the passage it's currently on then?

Got it, function is needed for using code in things, that's helpful.

So the == "" || null thing wasn't needed? That's how I got it to be aware that the input field was blank.

I got an error that '$' is undefined, why would that be?

Thank you for your help!
by (63.1k points)
edited by

I don't know why $ is undefined. Try replacing the $ with jQuery.

The code I posted (the first couple bits) does what you ask for in a more streamlined way (I think). It links to passage B (or any passage) checks the name and replaces it if it's empty all in one step.

 

Edit:

So the == "" || null thing wasn't needed? That's how I got it to be aware that the input field was blank.

Null is always false.  So no, that code doesn't do what you think.  You're basically saying 'if the text field is empty or if null is true...' which will always yield false because null is never true.  You probably meant:

if (character.name === '' || character.name == null) {...

which results in: if character.name is empty, undefined, or null.

I'm saying to do this:

if (!character.name) { ... 

which results in: if character.name is empty, undefined, null, false, or 0, i.e. if the value of the variable is 'falsy'.

by (63.1k points)
edited by
by (2.2k points)
Thanks Chapel! It works!
0 votes
by (23.6k points)

Can't you just put a simple <<if>> clause at the start of passage 2?

<<nobr>>
<<if $character.name == "">>
<<set $character.name to "somename">>
<</if>>
<</nobr>>

That seems to be the simplest solution. You could also just use the link macro:

<<link [[linkText|passageName]]>>
<<if $character.name == "">>
<<set $character.name to "somename">>
<</if>>
<</link>>

 

by (2.2k points)
oops, sorry, I meant snowman not sugarcube. I changed it above. It's a different format. Sorry about that.
...