ErHunt
ErHunt

Reputation: 311

Calling different function depending on which field has input

I need to create a calculator which call different function depending on which field has entry in it.
"initial_hosts" has to have an entry then the user can enter values in "Time" OR "Num" but not both (error message).

If a Value is entered in "Time" when the "Calculate" is clicked I would like it to call function number() and if "Num" call function timeCal()

<script language="javascript" type="text/javascript">
  function timeCal(){
    a=Number(document.calculator.initial_hosts.value);
    b=Number(document.calculator.Num.value);
    // Example of cal
    c=a*b;
    document.calculator.total.value=c;
  }
</script>

<script language="javascript" type="text/javascript">
  function number(){
    a=Number(document.calculator.initial_hosts.value);
    b=Number(document.calculator.Time.value);
    // Example of cal
    c=a*b;
    document.calculator.total.value=c;
  }
</script>

<div class="figure2"> 
  <form name ="calculator" class="form">
    <p>You can use the calculator below to see the approximate number of infected hosts after a given amount of time </p>
    <label for="Fname">initial hosts:</label><br/>
    <input type="text" name="initial_hosts"/><br/>

    <label for="Fname">Trigger Date:</label><br/>
    <input type="text"  id="TTime" value="" />
    <br/><br/>

    <font face="arial, helvetica" size="2" >Note: Enter the time space or the number of targets </font><br/>

    <label for="Time">Enter Time:</label><br/>
    <input type="text" name="Time"/><br/>

    <label for="Num">Target Number :</label><br/>
    <input type="text" name="Num"/><br/>

    <label for="total">Outcome:</label><br/>
    <input type="text" name="total"/><br />
    <br/>

    <p><input type="button" style="height: 35px; width: 100px" value="Calculate"  onclick="javascript:timeCal();"/>
  </form>
</div>

I have no idea how I can go about this, Thank you in advance :)

Upvotes: 0

Views: 483

Answers (2)

sarwar026
sarwar026

Reputation: 3821

<script language="javascript" type="text/javascript">  
    function callAppropriateFunction(){
        numValue=document.calculator.Num.value;
        timeValue=document.calculator.Time.value;  
        if(numValue!='')
            number();
        else if(timeValue!='')
            timeCal();
        else
            alert("please select either number or time");
    }
</script>


<input type="button" style="height: 35px; width: 100px" value="Calculate"  onclick="javascript:callAppropriateFunction();"/>

Upvotes: 1

Ibu
Ibu

Reputation: 43840

Create a new function called calculate(); and from there you check which field has data in it.

function calculate() {
   if (document.calculator.time.value != ""){
       timeCal();
   }else {
       number();
   }
}

and change your button to run calculate();

<input type="button" style="height: 35px; width: 100px" value="Calculate"  onclick="javascript:calculate();"/>

Upvotes: 0

Related Questions