1

I'm just starting to learn Javascript and following a tutorial. I've copied this code verbatim but it's just not doing what it's supposed to be doing. The video is from 2015 so maybe something about the language has changed? I have no idea, any help will be appreciated, thanks

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Example</title>
  <script type="text/javascript">
    function substitute() {
      var myValue = document.getElementById('myTextBox').value;

      if (myValue.length === 0) {
        alert('Please enter a real value in the text box!');
        return;
      }

      var myTitle = document.getElementById('title');
      title.innerHTML = myValue;

    }
  </script>

</head>
<body>
  <h1 id="title">JavaScript Example</h1>

  <input type="text" id="myTextBox" />
  <input type="submit" value="Click Me" onclick="substitute();" />

</body>
</html>
2
  • 1
    Your code seems fine. What errors are you getting? Commented May 21, 2017 at 18:56
  • 1
    I have tested this, and it works correctly. Suggesting it gets closed. Commented May 21, 2017 at 18:58

2 Answers 2

1

make it,

<input type="text" id="myTextBox">
<input type="submit" value="Click Me" onclick="substitute();">

You created a function substitute but trying to call substitut.

Sign up to request clarification or add additional context in comments.

3 Comments

The <input> tag has no closing slash.
yep. that one too.
I noticed the substitute thing, but that didn't change it. And removing the slashes doesn't seem to do it either. It might be worth noting I'm working and previewing with brackets.
0

Variable title hasn't been defined. You should change it to this:

myTitle.innerHTML = myValue;

2 Comments

It works with title.innerHTML as well. Did you try running code in browser?Line var myTitle = document.getElementById('title'); is not being used though.
I had not, but I just did and it works as you said. I don't understand how?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.