It looks like you're new here. If you want to get involved, click one of these buttons!
window.replace = function() { if (annyang) { var commands = { 'Yes': function() { alert('Hi! I can hear you.'); console.log('Hi! I can hear you.'); document.getElementById('valor').innerHTML="Yes"; } }; annyang.addCommands(commands); annyang.start(); } };
getElementById('valor').innerHTMLAn error appears saying that:
Cannot read property 'innerHTML' of null
(set: $mytext to "initial") <p id="valor">$mytext</p> (set: $varnextpassage to document.getElementById('valor').innerHTML) (if: $varnextpassage is "Yes")[(goto:"Segunda Pagina")] this is the value: $varnextpassage <input onmouseover="replace()" value="Speakingggggg" type="button"></input>
document.getElementById('valor').innerHTML
(set: $mytext to "initial") <p id="valor">$mytext</p> (set: $varnextpassage to $('#valor').text()) (if: $varnextpassage is "Yes")[(goto:"Segunda Pagina")] This is the value: $varnextpassage <input onmouseover="replace()" value="Speakingggggg" type="button"></input>
$('#valor').text()
Comments
1. Harlowe processes all the commands within the contents of a Passage before adding the generated output to the DOM (web-page), this means that the p element wont exist when the $('#valor').text() within the (set:) is executed.
2. When the (if:) is executed the text of the p element equals "initial", so even if you later updated the p element's text the (if:) has already determined that $varnextpassage did not equal "Yes".
Using some of Harlowe's built in features:
Thank you for your advice but that doesnt solve my problem.
My replace function is note the provided by twine.Lets call it myreplace
This function changes the value of my paragraph tag when the computer hears the word "yes": So when it changes I want to check its value, and if the value is yes, I want to go to the next passage. And I actually would like to check it without pressing any button.
So using the code you gave me I have done this:
But when calling the function it just changes the value of the text within the <p> tag, and I want it to change the value of $mytext to use it in the if condition. But I thought it was not possible as that function is in my Javascript story and I dont know if it can changes that variable from there.
So that Is why I was trying to the the if condition like this: So instead of reading the variable I read the text of the tag. because as I told you the function myreplace just changes the value of the <p> tag but not the value of $mytext.
Do you know how to solve this problem?
Thank you very much for your help!
Javascript code
This is my function that replaces the text:
This is the second function that returns the value of the text in the <p> tag
Passage code:
So in this part the value of $mytry never changes. But when I click in the console log button, on my console appears At the beggining, and when i replace the text appears So why it is working when I call it in console log?
I guess it is because the DOM is not loaded. Is there a way to do something like this in my passage? A way to wait until the DOM is loaded and then do it?
Thank you!
I understood your original post, explained why it was not going to work using those techniques and was suggesting that you look at Harlowe's built-in features to find a solution.
Like the following: The (live:) macro part reformatted for readability:
Re: your "A way to wait until the DOM is loaded and then do it?" question.
A common way to do this is to use Javascript's setTimeout function: