Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation URGENT: Error In Code, Can SOmeone Please Unbug?

    Hi Guys,
    Could Some One Please Run This Piece of Code And Debug It For Me, I'm Using Frontpage And Keep Getting A 'NULL' Error, It's Kinda IMportant So If Some One Could Do It Quick That Would Be Great, Also If POssible Could You Leave Some Comments Within The Script Where The Errors Are?
    Code:
    <script>
    var arr=['Product Added :','Quantity : ','Price : ','Total Value : ']
    
    var myItem =  new Item("Name", 5, 15.34)
    
    
    function Item(p_productName, p_quantity, p_price)
    {  // *** Use: var myItem = new Item('Name', 5, 15.34); ***
     this.product = p_productName;
     this.price = Number(p_price);
     this.quantity = Number(p_quantity);
     this.valueInStock = this.price * this.quantity;
    }  // *** End Object Definition: Item ***
    
    function addItem()
    {
     /**************************
      *** Local Variables ***/
    
     var prod; // *** Product name inputted by user ***
     var quan; // *** Quantity of product inputted by user ***
     var pric; // *** Price of product inputted by user ***
     var myItem; // *** this is the new object ***
     var root; // *** DOM Node for displaying everything ***
     var i; // *** Iterator ***
     var s; // *** DOM Node for displaying a line ***
    
     // End Local Variables 
    
     prod = prompt('Please Enter Product','');
     quan = prompt('Please Enter Quantity','');
    
     while (Number(quan) < 0)
     { // *** Prevent negative quantity ***
       quan = prompt('No negative numbers allowed. Please Enter Quantity','');
     }
    
     pric = prompt('Please Enter Price','');
    
     while (Number(pric) < 0)
     { // *** Prevent negative price ***
       pric = prompt('No negative numbers allowed. Please Enter Price','');
     }
    
     myItem = new Item(prod, quan, pric);
     root = document.getElementById('mydiv');
     i=0;
    
     for (arg in myItem)
     {
       s=document.createElement('span'); // *** Create a new line ***
       s.style.fontWeight='bold';
       s.appendChild(document.createTextNode(arr[i])) // *** Add the heading from arr ***
       root.appendChild(s); // *** Add the line to the display ***
       root.appendChild(document.createTextNode(myItem[arg])) // *** Add the text from myItem ***
       root.appendChild(document.createElement('br'))
       i++;
    
       if (i == arr.length)
         root.appendChild(document.createElement('br'));
     }
    
     if (confirm('Do you want to add another item?'))
     { // *** If the user wants to add another, recursively call this function 
       addItem();
     }
    }
    
    onload = addItem; // *** make the script run at page load 
    </script>
    <script>
           document.write("<strong>Product Added :</strong> " + myItem.prod + "<br />");
           document.write("<strong>Quantity :</strong> " + myItem.quan + "<br />");
           document.write("<strong>Price :</strong> $" + myItem.pric + "<p>");
           document.write("<strong>Total Value :</strong> $" + myItem.valueInStock + "");
    </script>
    Cheers,
    --==::SATRAH::==--

  • #2
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Anybody...please? lol

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like your error comes from:

    root = document.getElementById('mydiv');

    I am not sure what it is intended to reference?

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You have not been attentive to my code... root is a variable and it should be found as a div with id="mydiv". You simply have omitted to write it

    <body>
    <div id="mydiv"></div>
    </body>

    And remove
    <script>
    document.write("<strong>Product Added :</strong> " + myItem.prod + "<br />");
    document.write("<strong>Quantity :</strong> " + myItem.quan + "<br />");
    document.write("<strong>Price :</strong> $" + myItem.pric + "<p>");
    document.write("<strong>Total Value :</strong> $" + myItem.valueInStock + "");
    </script>

    Be cause you intend to write some variables which has not yet set. I have told you for a million times, document.write() is not a dynamic method
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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