Javascript Error?

_whelp

New Member
Reaction score
54
I made this prank webpage... but when I click the button nothing happens, and when I look in the Error Console in Firefox, it says "document.txtbox is undefined." Any help?
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" 
"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>
		Find your match here!
		</title>
		<script language="JavaScript" type="text/javascript">
			function FindMatch() {
alert("Your perfect match has been found, " + this.name);
				document.matchimage.src = 
"http://naturalpatriot.org/wp-content/uploads/2008/12/tall-hamburger.jpg";			
			}
			function Match(name) {
				this.name = name;
				this.FindMatch = FindMatch;
			}
		</script>
	</head>
	<body>
		<img name="matchimg" />
		<br />Enter your name here!
		<input type="text" name="txtbox" size="20" />
		<input type="button" name="btn" value="Find your match!" 
onClick="user = new Match(document.txtbox.value); user.FindMatch();" />
	</body>
</html>

err...
 

Samuraid

Advisor
Reaction score
81
You should try assigning an id to the text box and using getElementById() instead. It tends to be more reliable.

Code:
....
		<input type="text" name="txtbox" id="txtboxID" size="20" />
		<input type="button" name="btn" value="Find your match!" 
onClick="user = new Match(document.getElementById('txtboxID').value); user.FindMatch();" />
....
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
The alternative would be to write valid code...

txtbox isn't part of document.
It's part of the form that isn't there but really should.

Code:
<form name="myform" method="post" action="#">
<input name="myinput" id="myinput" ... >
...
</form>

document.myform.myinput.value (don't use)
document.forms["myform"].elements["myinput"].value (preferred "form" way)
document.getElementById("myinput").value (preferred overall)


document.matchimg doesn't exist either... use an id there too.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top