...

View Full Version : URGENT: Error In Code, Can SOmeone Please Unbug?



satrah
05-26-2006, 05:09 AM
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?

<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,:thumbsup:
--==::SATRAH::==--

satrah
05-26-2006, 05:29 AM
Anybody...please? lol:confused:

ezra143
05-26-2006, 08:10 AM
It looks like your error comes from:

root = document.getElementById('mydiv');

I am not sure what it is intended to reference?

Kor
05-26-2006, 12:26 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum