4 Replies - 1605 Views - Last Post: 30 March 2010 - 10:15 AM

#1 Guest_Stephanie*


Reputation:

Help with a function

Posted 29 March 2010 - 10:14 PM

I'm learning javascript and need help with this function. I keep getting an error on the iNum variable. I've tried so many combinations with the this keyword, but nothing seems to work.

The purpose of this function is to display the cost of the quantity of items ordered by a customer and to update the total cost of the order. The price of each item is stored in the priceitem field, where item is the item number. The cost of each item is stored in the priceitem field, and the cost is stored in the costitem field.

function calcCost() { 
   
   /* create a variable named iNum that is equal to the ID of the currently selected object,
      using the slice() method to slice off the first 3 characters from the ID. Hint: use the
      this keyword to reference the currently selected object
    */
    
   var iNum = this.id.value; 
   iNum.slice(3);
     
 /*create a variable name price that references the priceitem field, where item is the value
    of the iNum variable. Hint: use the document.forms[0].elements collection along with the
    field's name to reference the priceitem field
 */
    
  var price = document.forms[0].elements["price"+iNum];
  var qty = document.forms[0].elements["qty"+iNum];
  var cost = document.forms[0].elements["cost"+iNum];
   
  var reqty = /^\d+$/;
    if (reqty.test(qty.value)){
      cost.value = (price.value*qty.value).toFixed(2);
      calcTotal();
    }else{
      alert("Please enter a digit greater than or equal to 0");
    qty.value = 0;
    document.forms[0].qty1.focus();
    }
 }



Is This A Good Question/Topic? 0

Replies To: Help with a function

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Help with a function

Posted 29 March 2010 - 10:30 PM

It would help if we know what the error message in that you're receiving :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Stephanie*


Reputation:

Re: Help with a function

Posted 29 March 2010 - 10:54 PM

Sorry... the error says "this.id is undefined"

Thanks!! =)
Was This Post Helpful? 0

#4 jrm402  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 333
  • Joined: 18-March 10

Re: Help with a function

Posted 30 March 2010 - 08:18 AM

Are you trying to get the html id of the element?

Maybe posting more code would help us diagnose.
Was This Post Helpful? 0
  • +
  • -

#5 robhilly  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 26-May 08

Re: Help with a function

Posted 30 March 2010 - 10:15 AM

Within your function you have the comments specifying that the id property identifies the currently selected object:

   /* create a variable named iNum that is equal to the ID of the currently selected object,
      using the slice() method to slice off the first 3 characters from the ID. Hint: use the
      this keyword to reference the currently selected object
    */



However, nowhere in this function is the id property defined. Perhaps you need to specify it in your function argument list.

You should post more of the code so we get a better idea of what you are trying to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1