开发者

javascript function not calling possibly due to scope issue

开发者 https://www.devze.com 2023-01-19 09:21 出处:网络
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 bel

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>


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()"


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消