views:

33

answers:

2

I am looking to call my clear() JS function to clear the text values in some input fields on my webpage, but for some reason, the function doesn't appear to be called correctly. I believe this may be a scope issue, but I'm not sure.

Here is my markup, script and styling kept together for ease-of-use currently, but will be mended once scripting is finalized:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
function calculateQuad()
{
    var inputa = document.getElementById('variablea').value;
    var inputb = document.getElementById('variableb').value;
    var inputc = document.getElementById('variablec').value;

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa
    root2 = (-inputb + Math.sqrt(root))/2*inputa 

    document.getElementById('root1').value = root1;
    document.getElementById('root2').value = root2;
    if(root<'0')
    {
        alert('This equation has no real solution.')
    }
    else {
        if(root=='0')
        {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = 'No Second Answer'
        }
        else {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = root1
            }
        }
}
function clear()
{
    document.getElementById('variablea').value = "";
    document.getElementById('variableb').value = "";
    document.getElementById('variablec').value = "";
}
</script>
<style>
#container
{
    text-align: center;
}
</style>
</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
    a:<input id="variablea" value="" type="text">
    <br/>
    b:<input id="variableb" value="" type="text">
    <br />
    c:<input id="variablec" value="" type="text">
    <br />
    <input id="calculate" value="Calculate!" type="button" onClick="calculateQuad()">
    <input id="erase" value="Clear" type="button" onClick="clear()">
    <br />
    <br />
    Roots:
    <br />
    <input id="root1" type="text" readonly>
    <br />
    <input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>
+1  A: 

You must rename the 'clear' method. Try naming it 'clearFields' or something. (;

Also, if you only want to reset the form fields you can also do this: onClick="this.form.reset()"

aLfa
Aghhhh!! :D Thanks for pointing that out. Why is there the need for the rename though? Is it reserved, or duplicate name, or what?? hahaThanks for the alternate and easier suggestion too.
BOSS
Yes, 'clear' is kind of reserved on this context. You are welcome. (:
aLfa
+1  A: 

You can avoid the issue with function naming entirely by using:

document.getElementById("calculate").onclick = function () {
    ...
};

document.getElementById("erase").onclick = function () {
    ...
};

This is actually a preferred method of adding event handlers by web developers because it avoids cluttering the HTML code with inline snippets of JavaScript while retaining cross-browser compatibility.

idealmachine
Thanks! I like this a lot as well, if using plain ol' JS, it makes for way less unobtrusive code.
BOSS