0

I am constructing a simple calculator using a form that requires selections from a drop down which when run inputs 3 different calculations into 3 different fields. Anyway -- I have put some js in the code to prevent invalid selection combinations, using an alert to inform the user. What I would like to do is reset only the inputs (changing none of the drop downs when the user clicks "ok" on the alert box. I'm a newbie, tried several things and not come close. Any help?

<script type="text/javascript">
function clearInputs() {
    document.getElementById("RawCapacity").value = "";
    document.getElementById("RAIDCapacity").value = "";
    document.getElementById("WindowsCapacity").value = "";
    }

function RAIDcalc() {
    //Values put into the calculator
    var A = document.forms[0].DriveCapacity.value *1;
    var B = document.forms[0].NumberOfDisks.value *1;
    var C = document.forms[0].RAID.value *1;
    var D = document.forms[0].HotSpare.value *1;

    //Pre-math for the calculations 
    var E = C + D;
    var F = B - E;
    var G = A * 0.95;

    var H = document.getElementById("RAID").options[document.getElementById("RAID").selectedIndex].text

    //Validate form (to ensure selections are made), 
    //validate values (to prevent impossible combinations), 
    //and to calculate and write the responses
    if (A == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (B == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (C == "null") {
        alert("Please make a selection in the required fields.");
        clearInputs();
        }
    else if (H == "RAID 5" && B<=(D + (1 * 1))) {
        alert("This configuration has too many Hot Spares.");
        }
    else if (H == "RAID 6" && B<=(D + (2 * 1))) {
        alert("This configuration has too many Hot Spares.");

    else if (H == "RAID 6" && B<4) {
        alert("RAID 6 requires a minimum of FOUR disks.");
        }
    else {
        document.forms[0].RawCapacity.value = Math.round(((A * B) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";   
        document.forms[0].RAIDCapacity.value = Math.round(((F * A) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";  
        document.forms[0].WindowsCapacity.value = Math.round(((F * G) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";
        }
    //For testing
    //alert("A="+A+" B="+B+" C="+C+" D="+D+" E="+E+" F="+F+" G="+G);
    //var RAIDtext = document.getElementById("RAID");
    //var RAIDselindex = document.getElementById("RAID").selectedIndex;
    //alert (document.getElementById("RAID").options[document.getElementById("RAID").selectedIndex].text);
    //if (H == "RAID 6" && B<4) alert("H = RAID 6 & B<4!");

    }
</script>
3
  • Note: if (A = "null") should be if (A == "null") same for B and C Commented Sep 12, 2011 at 21:11
  • alert (like confirm, prompt, etc) is synchronous. The user either hits "ok" or leaves the page. As such, you just need to add code directly after the call. Commented Sep 12, 2011 at 21:15
  • reposted code -- now broken??? Commented Sep 12, 2011 at 21:32

3 Answers 3

1

Add a function to clear the input fields:

function clearInputs() {
    document.getElementById("RawCapacity").value = "";
    document.getElementById("RAIDCapacity").value = "";
    // The same for other fields
}

and call it along with each alert() call:

if (A == "null") {
    alert("Please make a selection in the required fields.");
    clearInputs();
}

(Note: the condition in the if should use the == operator, which is used for comparison. = is for assignments.)

By the way, your else block is missing the curly braces, it should say:

else {
    document.forms[0].RawCapacity.value = Math.round(((A * B) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";   
    document.forms[0].RAIDCapacity.value = Math.round(((F * A) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";  
    document.forms[0].WindowsCapacity.value = Math.round(((F * G) / 1000)*Math.pow(10,2))/Math.pow(10,2) + " TB";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hmmm, I think I broke something trying to impliment your solution:
You are missing the closing brace before the line else if (H == "RAID 6" && B<4) {.
By the way, your browser catches syntax errors like this -- be sure to first check the JavaScript console for error messages, if something isn't working.
Works great now. I tried the js console on Web Developer, but I'm such noob it didn't make much sense to me. Thanks so much for all your help.
0

Do you mean confirm()? alert() doesn't return a value, but confirm() does:

if (confirm("Do you want to do something?")) {
  //Ok was pressed.
}
else {
  //Cancel/close was pressed.
}

1 Comment

I'm not really interested in offering options. I just want on the alert close to manipulate the input text fields of the form, but no other parts of the form.
0

Note: if (A = "null") should be if (A == "null") same for B and C

To reset the textboxes, you can do

 if (A == "null") 
 {
        alert("Please make a selection in the required fields.");
        clearTextBoxes();
 }

where clearTextBoxes() would be something like

function clearTextBoxes()
{
     document.forms[0].RawCapacity.value = "";
     document.forms[0].RAIDCapacity.value = "";
     document.forms[0].WindowsCapacity.value = "";

}

1 Comment

-1 You still used the wrong operator in your code example, as well as an undefined function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.