|
'reset', 'clear' aren't reserved words...you've just run afoul of a form element's scope chain, the list of objects checked to resolve a variable's (property) value. For HTML form elements, the scope chain is:
1) event handler itself (function)
2) element object
3) Form object
4) document object
4) window object
...in that order. Before your function call ever gets to the window (which holds your reset function - window.reset) it finds Form.reset and runs that, resetting the form. Similarly, clear() resolves to document.clear(). If you want them to resolve to window properties, you need to bypass the scope chain by explicitly referencing the window object:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
function reset() {
alert('My "Reset" called.');
document.getElementById("add").elements["num1"].value = "";
document.getElementById("add").elements["num2"].value = "";
}
function clear() {
alert('My "Clear" called.');
document.getElementById("add").elements["num1"].value = "";
document.getElementById("add").elements["num2"].value = "";
}
</script>
</head>
<body>
<form name="add">
<input type="text" name="num1" value="default" /><br />
<input type="text" name="num2" value="default" /><br />
<input type="button" value="Reset" onclick="window.reset()" />
<input type="button" value="Clear" onclick="window.clear()" />
<input type="reset" value="A Real Reset Button" />
</form>
</body>
</html>
Better approach: avoid DOM objects, use safer naming conventions!
|