Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 4.33 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2003, 02:46 AM   PM User | #1
Saj
Regular Coder

 
Join Date: Feb 2003
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Saj is an unknown quantity at this point
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?
__________________
- Saj

Last edited by Saj; 05-14-2003 at 02:50 AM..
Saj is offline   Reply With Quote
Old 05-14-2003, 03:00 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
reset() and clear() are reserved words.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 05-14-2003, 03:57 AM   PM User | #3
cheesebagpipe
Regular Coder

 
Join Date: Nov 2002
Posts: 596
Thanks: 0
Thanked 0 Times in 0 Posts
cheesebagpipe is an unknown quantity at this point
'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!
cheesebagpipe is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:03 AM.


Advertisement
Log in to turn off these ads.