Posts: 361
Name: El Phantasmo
Location: England, north west
|
Ive recently bought a book to help me to learn Javascript. Theres a couple of issues Im having though, mainly when creating functions.
Here is a little practise script I made just to check the value of some text fields:
Code:
function validate (field)
{
value = field.value;
window.alert (field + "'s value is <b>" + value + "</b>");
}
and here is the HTML to call the script:
Code:
<!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=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>Field 1
<input name="field1" type="text" id="field1" value="sdfsdfsd" size="20" maxlength="20" />
</label>
</p>
<p>
<label>Field 2
<input name="field2" type="text" id="field2" value="23432424" size="20" maxlength="20" />
</label>
</p>
<p>
<input name="testField1" type="submit" id="testField1" value="Test Field 1" onclick="validate('field1');" />
<input name="testField2" type="submit" id="testField2" value="Test Field 2" onclick="validate('field2');" />
</p>
</form>
</body>
</html>
When I click any of the buttons, Im presented with a message similar to the following:
Code:
Field 1's value is undefined
If I specify in the script which text field I want to be checked, then it works. So I think the problem is in this bit of the script:
Code:
value = field.value;
But I cant see how I could fix it? I wanted to be able to pass arguments to the function to make it more flexible.
|