Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?
    Last edited by Saj; 05-14-2003 at 02:50 AM.
    - Saj

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    reset() and clear() are reserved words.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    '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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •