Results 1 to 3 of 3

Thread: How to access value

  1. #1
    Join Date
    Dec 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to access value

    I am trying to write a function that can be used by a number of diiferent sections of code. Here is what I have
    Code:
    function update(numb) {
      var x;
      var box = new Array();
      box[1] = "price1";
      
      ttl =  document.forms["product"].box[numb].value;
      document.forms["product"].total = ttl;
    }
    Then I call it with
    Code:
    <input type="text" id="price1" onkeyup="update(1)">
    The problem is that it doesn't work. If I change this line
    Code:
    ttl =  document.forms["product"].box[numb].value;
    to
    Code:
    ttl =  document.forms["product"].price1.value;
    then it works. Can anyone please point out my mistake?

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by patter View Post
    I am trying to write a function that can be used by a number of diiferent sections of code.
    Why not start by stating exactly what you want to create. Identifying the current problem is simple, but correcting that may not put you on the right track to solving the real issue.

    Code:
    function update(numb) {
      var x;
    Presumably that should be 'ttl' not 'x', or the identifier used below - 'ttl' - should be 'x'.

    Code:
      var box = new Array();
      box[1] = "price1";
      
      ttl =  document.forms["product"].box[numb].value;
    You haven't really considered what that member expression means.

    1. Resolve Identifier 'document': document object.
    2. Get property of 'document' named 'forms': forms collection.
    3. Get property of 'forms' collection named 'product': form object.
    4. Get property of form object named 'box': undefined (there is no such property).
    5. Fatal error: attempting to access a property ('1') of undefined.


    document.forms["product"].total = ttl;
    That also seems to be a mistake: you should assigning to the value property of the form control. Certainly, don't create a property on the form object itself.

    <input type="text" id="price1" onkeyup="update(1)">
    Don't update whilst the user is editing. It'll only cause problems if the user enters invalid data. Instead, do it in response to the change event. Even better: create a button specifically for the action.

    If this is for an order form, it should all be implemented server-side and this update process can be omitted entirely - the server can show the total cost during the confirmation process.

    If I change this line

    Code:
    ttl =  document.forms["product"].box[numb].value;
    to

    Code:
    ttl =  document.forms["product"].price1.value;
    then it works. Can anyone please point out my mistake?
    Use bracket notation create the property name:

    Code:
    ttl = document.forms.product.elements['price' + numb].value;
    Mike

  3. #3
    Join Date
    Dec 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for the in-depth explanation. I was able to get it to work with that.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •