2

I'm making my first text-based interactive scenario, I want the user to enter whatever they want and click Submit. There is just title, some scenario text, a typing space and button to Submit, however when I press the submit, it always gives error that the "output" is not specified.

I've established I can use functions from HTML in JS with the "getElementById", however this doesn't seem to work the other way around. (Also I'm using the integrated browser emulation in Scrimba). If anyone can tell me a good software they use with live representation of the code that will be huge help too.

I've tried moving the <script type="text/javascript" src="index.pack.js"></script> from the head, to the body, to the end of the body, and that doesn't change anything.

I've also tried to change the onclick and do the entire process in JS, then just display the new text by refreshing the window, but then clicking showed no response and not even an error at all, so it was likely a wrong code. This is why I'd much prefer to change as little as possible about the current layout.

var userAnswer = document.getElementById("userAnswer").value;
function output(userAnswer) {
    var answer1 = "";
    switch(userAnswer) {
     case 1:
     case "What is its name?":
     case "Ask for name":
        answer1 = document.getElementById("outputText").innerHTML = "Yes"
        break;        
    }
    return answer1;
}
<h1>The Creature Trial</h1>
<p id="outputText"><em>A creature of inconceivable dimensions encountered...</em></p>
<textarea placeholder="What do you do..." id="text" rows="5" cols="25"></textarea>
<input id="userAnswer" type="button" value="Proceed" onclick="output(userAnswer)">
<script type="text/javascript" src="index.pack.js"></script>

Obviously in this example I'm just trying to change the text to Yes.

Above the JS code shown here, are the 118 objects and some vars, nothing important to the problem.

What I want to achieve is receive the userAnswer, and from that, find certain specified keywords that link to the JS objects, and based on those keywords change the outputText "A creature of inconceivable dimensions encountered..." to the appropriate response I choose, in this case "Yes".

Instead, nothing happens as I click, and I receive this message in the console:

ReferenceError: output is not defined (https://run.scrimba.com/static/cdPLKJh8/-1/index.html:1)

EDITS: 1. Thanks to Masoud Keshavarz for editing this post, and making its formating less of a mess.

  1. To clarify, the HTML code and the JS code are external files, they are completely separate.

  2. I did replace the "userAnswer" references with "text", as I was originally mistakenly referring to the button not the text that was inputted.

  3. Finally I found that after the now-Approved comment edited my code it stopped showing the error, but it truly worked when I changed the number 1 in case 1 of the switch, to a string rather than a plain number (adding quotes around it). This is still strange and I don't understand why I can only use strings not numbers but it works.

The only problem with this solution is that it doesn't solve the connection problem between the two files HTML and JS, but rather it combines them in one. I would really like to have them separate or at least find out if that is possible or not?

  • You’re getting the value from the userAnswer button, which is just "Proceed". You don’t check for this in the switch statement cases. Also your return statement seems to be outside the function. Is the JS in the separate file or is it just below the HTML - if in the same file it will need tags. – Chris Aug 25 '19 at 01:38
  • I think you expect userAnswer to refer to the text area but it refers to the button. – Chris Aug 25 '19 at 01:41
  • 1
    Thank you for your contribution, you are right about me targeting the button and not the user input, and that has now been fixed. The return statement is outside the switch, but not outside the function, so it works this way, at least from what I can see, though I understand different software implement slightly different rules so I'll keep it in mind. – Dimh Doychev Aug 25 '19 at 04:30

2 Answers2

1

I changed a little bit your js code like this

function output() {
let userAnswer = document.getElementById("text").value;
    var answer1 = "";
    switch(userAnswer) {
     case 1:
     case "What is its name?":
     case "Ask for name":
        answer1 = document.getElementById("outputText").innerHTML = "Yes"
        break;        
}
return answer1;
}

And at your html I changed the onclick to call the function without parameter

<input id="userAnswer" type="button" value="Proceed" onclick="output()">
Fabio Assuncao
  • 624
  • 4
  • 12
  • Your solution still results in the same error, and I made sure I replaced it correctly, but thanks for trying nevertheless. Did you test this on your software because if it worked there then maybe this is Scrimba's fault somehow? Let me know. – Dimh Doychev Aug 25 '19 at 03:41
  • I tested it on jsfiddle and it was fine there – Fabio Assuncao Aug 25 '19 at 07:49
1

Three issues:

  1. You're getting the value of the wrong element. The text you're looking for is in the text element, which is a textarea, and not the userAnswer element, which is a button.

  2. You're getting the value of the element at the wrong time - when the script loads, not when the user clicks the button. When the script loads, the value of the text element is blank. To fix this, move the var userAnswer... line inside the function.

  3. You don't need to pass an output variable to the function if you're using getElementById to get the value. It doesn't hurt to leave it in, but it's not necessary.

Here's the result:

function output() {
    var userAnswer = document.getElementById("text").value;
    var answer1 = "";
    switch(userAnswer) {
     case 1:
     case "What is its name?":
     case "Ask for name":
        answer1 = document.getElementById("outputText").innerHTML = "Yes"
        break;
     }
     return answer1;
}
<h1>The Creature Trial</h1>
<p id="outputText"><em>A creature of inconceivable dimensions encountered...</em></p>
<textarea placeholder="What do you do..." id="text" rows="5" cols="25"></textarea>
<input id="userAnswer" type="button" value="Proceed" onclick="output()">
<script type="text/javascript" src="index.pack.js"></script>
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
Dan B.
  • 1,451
  • 2
  • 14
  • 23
  • Thank you, at first it looked like your solution only removed the error message, but nothing was happening. Then I made sure I fixed all "userAnswer" to "text", still nothing. Finally, I changed the 1 in "case 1" of the switch function, to a string value rather than a number and inputting that worked. I still don't understand why I have to use a string and not a number but anyway, thank you, this worked, and thanks for pointing out all of my mistakes. – Dimh Doychev Aug 25 '19 at 04:10
  • Actually, it only works if I put the JS code in the HTML file, not if I have the same code externally, so it did make it work, but it didn't fix the issue of linking the two files together. Can you help me with that too? – Dimh Doychev Aug 25 '19 at 04:38
  • Where is index.pack.js? Is it in the same directory as the HTML file? If you try to open it in your browser by itself, does it load? – Dan B. Aug 25 '19 at 10:34
  • nvm, I ll keep everything in one file since Im tired of dealing with issues – Dimh Doychev Aug 26 '19 at 21:41