You didn’t state how that HTML structure is being added to the page, so I will assume that it is contained within either:
the Passage being visited.
a Passage that is being included by the one being visited.
one of the special passages like StoryCapion, PassageHeader, or PassageFooter.
a Passage being displayed within a dialog that is being displayed using via the Dialog API.
First a little background about when specific parts of Story HTML file are processed / executed:
the contents of standard passages aren’t processed until they are visited, or included inside such a Passage. This means that any HTML elements or structures being created by a Passage doesn’t exist until that point in time.
HTML generated by a Passage being visited are first added to a buffer, that is only added to the Document Object Model (DOM) of the page once all of the contents of that Passage or any it includes has finished being processed. This means that any JavaScript executed directly during that processing won’t be able to locate the new elements because they won’t exist within the DOM yet. So such execution needs to be delayed some how so the DOM has time to be updated.
the contents of the Story JavaScript area, which includes that of any JS files that were included in the “source path” of a Twee Notation based project, is processed / executed during the SugarCube Runtime Engine’s start-up process. This means that that JavaScript can’t directly reference any HTML element / structure generated by a Passage, because such are all processed after the Story JavaScript area.
So, based on the information you’ve supplied the digit and fa-arrow-left-long classed elements you’re trying to locate done exist at the time your JavaScript is being executed.
If you want to keep defining that JavaScript code inside your js file then simply wrap it within a function like so…
setup.meaningfulMethodName = function () {
/* JavaScript that binds listeners to the "keys" of the "Keypad". */
};
And then use a <<run>> or <<script>> macro within the Passage that contains the HTML structure to execute that method, making sure to either:
wrap that macro call within a <<done>> macro so its execution is delayed enough for the structure to of been added to the DOM.
place that macro call within one of the special passages, like PassageDone, whose contents gets processed after the processing of the visited Passage has finished.
eg. If the method is to be called within the visited Passage.
You examine the keypad...
/* The HTML structure being used to display the "Keypad" */
<<done>><<run setup.meaningfulMethodName() >><</done>>
I should have done that. yes. It is included in a passage that displays inanother one.
so you say i have to give my two functions a wrapper and then add a <<run>> on the passage that contains the html?
then a little followup question. if I want to evaluate the added numbers
i could do so by adding another fuction to it?
like
$(".fa-phone").on('click', function () {
var check = ($('#KeyPadOutput').childen().contents().unwrap());
if (check === State.variables.password) { /* Code to replace content */};
};`
Assuming I can access the variables of sugarcube like this. But my Brain can’t wrap around how to use the <<replace>> function inside of javascript.
Technically SugarCube’s jQuery.wiki() method can be used to call macros within JavaScript that is executed within SugarCube’s Private Scoped Context.
$(".fa-phone").on('click', function () {
var check = ($('#KeyPadOutput').childen().contents().unwrap());
if (check === State.variables.password) { $.wiki('<<somemacro>>'); };
};
However, as you are already using jQuery in your event handle, you could use that library’s <jQuery>.text() (Textual content) or <jQuery>.html() (elements and textual) methods to update the content of the relevant identified area of the page.
The jQuery.wiki() method I linked to previously is found in the jQuery Methods section of SugarCube’s documentation. That same section includes a <jQuery>.wikiPassage(name) method, whose documentation includes an example that demonstrates how to append the contents of a child Passage to an identifiable element.
note: jQuery is designed so methods that belong to the same selector result can be called in sequence without needing to obtaining that result more than once. This behaviour is often known as chaining (methods).
eg. in your example you wanted to call both <jQuery>.empty() and <jQuery>.append() on the same contents identified element, so that element’s current contents could be replace with something else.
$('#contents').empty().append('new content');
The SugarCube JavaScript equivalent of your <<replace>> macro based example would be…
$('#contents').empty().wikiPassage('Name of child Passage');