0

Seems like this is a pretty common error, but being that I'm new to this I have no idea how to actually fix it. It's suppose to change the text on the page to what's in the input box once I click the button. Files are in the right directories so I know that's not the problem. HTML Code:

<!doctype html>
<html>
  <head>
    <title>Learning JavaScript</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script type="text/javascript" src="JS/index.js"> </script>
  </head>

  <body>
    <input id='myInput' type='text' value='test' />
    <button id='stylesChanger'> Change the text!</button>
    <div id='firstDiv'>This is some text </div>
  </body>
</html>

Javascript Code:

var newText=document.getElementById("myInput").value;

document.getElementById("stylesChanger").onclick=function() {
  document.getElementById('myInput').value;
  document.getElementById("firstDiv").innerHTML=newText;
}
Erazihel
  • 6,368
  • 3
  • 24
  • 49
lettda
  • 21
  • 8
  • Your code is working : https://jsfiddle.net/UQTY2/567/ – sdespont Jul 19 '15 at 18:25
  • try adding your `` after the `` ends. – Lal Jul 19 '15 at 18:27
  • 1
    @sdespont that's because jsfiddle is putting the code in a "load" handler. The code in the OP will run before the DOM is loaded. Also note that if you actually type something into the input and *then* click the button, you don't get the new value. – Pointy Jul 19 '15 at 18:28
  • Weird it just doesn't work for me when I open it into chrome – lettda Jul 19 '15 at 18:29

0 Answers0