i've got a project to be done using javascript and html....i don't know how to pass the arguments from <input type="text"> to the javascript function i'm using in my program. i just want the javascript script to calculate the input given by the user and return the answer.
here is the program:

Taxi Fare
<script lang="text/javascript">
// calculates taxi fare based upon miles traveled 
// and the hour of the day in military time (0-23).
var taxiFare = function (milesTraveled, pickupTime) {
  var baseFare = 2.50;
  var costPerMile = 2.00;
  var nightSurcharge = 0.50; // 8pm to 6am, every night

  var cost = baseFare + (costPerMile * milesTraveled);

  // add the nightSurcharge to the cost if it is after 
  // 8pm or before 6am
  if (pickupTime >= 20 || pickupTime < 6) {
      cost += nightSurcharge;

  return cost;
<input type="text" onclick="taxiFare()" value="Call function">
<input type="Submit" value="OK" onclick="taxiFare()">
<input type="reset" value="Clear">
<script type="text/javascript">
document.write("Your taxi fare is ₹" + taxiFare(5,2));

pls its really urgent...i need to submit it by 2morrow.....pls it would be of great help. Thanx.

Edited by Ezzaral: Code formatting. Also, please respect the rule about not using "text speak" on the forums. This is a forum, not SMS.

6 Years
Discussion Span
Last Post by MarPlo


If you add an ID attribte in that Input field, you can use this sintax in JavaScript to get the value of that input:

var valinp = document.getElementById('id_field').value;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.