File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Javascript" Watch "Javascript" New topic


arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
I have a shopping cart showing the list of books, each book has a textfield corresponding to it, where user can fill in the quantity of books. I have javascript to check if the user has filled in the correct quantity. it works fine when 1 book is added to cart. But gives error when multiple books are added to the cart.
Javascript :
function Check()
n = document.forms["addtocart"].quantity.value;

if(n.length == 0)
alert("Please enter some quantity");
// more code
i get following error in Intenet Explorer separate window with yellow sign at the side :
Problems with this web page might prevent it from displayed properly or functioning properly.
n.length might not be an object.
My Update button calls Check() function:
<input type="Submit" name="update" value="Update" onClick="Check()">
Quantity textfield is:
<input type="int" name="quantity" size="2" maxlength="2" value="<%= Integer.parseInt((String)request.getAttribute("quantity"))%>">
i am getting this error due to javascript. if somebody could figure out the problem. Please let me know.
srikanth bukkapatnam

Joined: Jul 31, 2001
Posts: 7
Try this :
var n = document.forms["addtocart"].quantity.value;
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
i get all the alerts if i add 1 book to the cart but doesn't work if add more than 1. this happens whether i put :
n = document.forms["addtocart"].quantity.value;
var n = document.forms["addtocart"].quantity.value;

thanks for your help
Aman Rustogi

Joined: May 11, 2001
Posts: 22
Arpit i feel the error is due to naming conflict.
Do all the text boxes where u enter the quantity have the same name? If that's the case, then the ERROR is bound to happen. U can avoid this by assigning different name's to each edit control and also test the object reference returned using 'eval()' in ur javascript function.
I hope this will resolve ur problem with my understanding.
I agree. Here's the link:
subject: Javascript
It's not a secret anymore!