View Full Version : javascript:reset()
The same as <input type="reset" />?? Because I have <input type="button" value="Reset" onclick="javascript:reset()" /> and it just resets the field...it doesn't load the pop-up like I ask it to. However, if I change the name of the function to repeat, it works. It doesn't even work with <input type="button" value="Reset" onclick="javascript:clear()" />
function reset {
document.getElementById("add").elements["num1"].value = "";
document.getElementById("add").elements["num2"].value = "";
calculate(x,y);
}
Is this supposed to happen?
glenngv
05-14-2003, 03:00 AM
reset() and clear() are reserved words.
cheesebagpipe
05-14-2003, 03:57 AM
'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!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.