Jump to content

Trouble with innerHTML functions


Recommended Posts

Why will this not work?java script:

// Javascriptfunction sayHi() {  var txtName = document.getElementById("txtName");  var divOutput = document.getElementById("divOutput");    var name = txtName.value;    divOutput.innerHTML = name;}

HTML Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>	<title>XHTML, CSS, and Javascript!</title>	<script src="xgame.js" type="text/javascript">	</script>	</head><body>  <form action="">    <fieldset>     <label>Anything here</label>     <input type="text" value="" id="textName" />     <input type="button" value="Release the monkeys!" onclick="sayHi()" />    </fieldset>  </form>    <div id="divOutput">    Watch this space!  </div></body></html>

The Firefox console says txtName is nullOn line 7

var name = txtName.value;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...