...

View Full Version : Beginner javascript - not working? :/



udjamaflip
08-26-2008, 02:35 PM
http://udjamaflip.com/test.html is the url



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
<!--

function validate_form ( )
{
valid = true;

if ( document.contact_form.name.value == "" )
{
alert ( "Please fill in the 'Your Name' box." );
valid = false;
}

return valid;
}

//-->
</script>


</head>

<body>


<form name="contact_form" method="post"action="test.html" onsubmit="return validate_form();">

name: <input type="text" name="name" />
<br />
age: <input type="text" name="age" />
<br /><br />
<input type="submit" value="submit" name="submit" />

</form>

</body>
</html>


Is the code

Anyone able to tell me whats wrong? it should be very basic and working :/

abduraooft
08-26-2008, 02:48 PM
document.contact_form.myname.value


<input type="text" name="myname" />
Never use an attribute name to name a control to confuse the DOM ( woops .. how can I express that ? ;))

Philip M
08-26-2008, 03:02 PM
[
Never use an attribute name to name a control to confuse the DOM ( woops .. how can I express that ? ;))

To develop that point:-

In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an element name or ID. You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/attributes such as 'name' or 'id' or 'value' or 'checked' or 'submit' or 'replace' or 'parseInt'.


A child of five would understand this. Send someone to fetch a child of five.
Groucho Marx

abduraooft
08-26-2008, 03:41 PM
...and you'd get a clear picture if you just put alerts like


alert(document.contact_form.name);
alert(document.contact_form.myname); //after changing the name of input



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum