473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

JavaScript woes

I have this javascript code that decided that it didn't want to work
anymore. At one point in time it did work. I don't remember changing
anything that would have caused it to stop working. But anyway, below
is the JavaScript code that is in a .js file:

by the way, the error is:
Error: 'elements[...].value' is null or not an object

function calcIt(name,thisForm,num) {
var num = (!num) ? 5 : num;
var val = 0 * 1;
var tot = name + "T";
var lcv = (!num) ? 1*1 : 0;

for (var i = lcv; i <= num; i++) {
var elem = name + i;

//This is where the error occurs
var qty = thisForm.elements[elem].value * 1;

if ( isNaN(qty) )
qty = 0;

if (qty < 0) {
alert("You cannot have a negative number");
thisForm.elements[elem].value = 0;
break;
}
val += qty;
}
thisForm.elements[tot].value = val;
}

And here is the HTML that's calling this function:

<input type="text" name="draft<%=i%>" size=2 maxlength=11
class="Normal" onChange="calcIt('draft',this.form);"></td>

<input type="text" name="final<%=i%>" size=2 maxlength=11
class="Normal" onChange="calcIt('final',this.form);"></td>

<input type="text" name="rep<%=i%>" size=2 maxlength=11 class="Normal"
onChange="calcIt('rep',this.form);"></td>
Any help would be appreciated.

Thanks,
- Jeremy
Jul 20 '05 #1
4 1246
jc***@belzon.com (Jeremy) writes:
by the way, the error is:
Error: 'elements[...].value' is null or not an object function calcIt(name,thisForm,num) {
var num = (!num) ? 5 : num;

You don't pass a number, so num=5.
var val = 0 * 1;
val = 0;
var tot = name + "T";
var lcv = (!num) ? 1*1 : 0;
Num is always "true" at this point, so lcv=1.
for (var i = lcv; i <= num; i++) {
.... so i goes from 1 to 5.
var elem = name + i; //This is where the error occurs
var qty = thisForm.elements[elem].value * 1;
Are you sure you have five of each element?
To convert to number, I prefer the Number function or the prefix +.
Multiplying by 1 just looks ... mistaken.
And here is the HTML that's calling this function:


No. That's not the HTML. It looks like some server-side script, but it
is not the HTML that is sent to the server, so it is not the HTML that
fails ... so we can't see what's wrong.

My guess is that you don't have draft1 to draft5 in the page, but I
have no way to check.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Thanks for the quick reply.
No. That's not the HTML. It looks like some server-side > script, but it is not the HTML that is sent to the server, so it is not the HTML that fails ... so we can't > see what's wrong.

Yes it is HTML. Last time I checked, <input type....> is an HTML tag.
The HTML's being generated dynamically via a client-side VBScript. But I
digress...

My guess is that you don't have draft1 to draft5 in the > page, but I

have no way to check.

I do have draft1 to draft5 on the page. that's what the
name="draft<%=i%>" is for. There's a loop that increments the lcv, i,
from 1 to 5 and prints it out each time.

Thanks again for the quick reply.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
> I have this javascript code that decided that it didn't want to work
anymore. At one point in time it did work. I don't remember changing
anything that would have caused it to stop working. But anyway, below
is the JavaScript code that is in a .js file: function calcIt(name,thisForm,num) {
var num = (!num) ? 5 : num;
var val = 0 * 1;
var tot = name + "T";
var lcv = (!num) ? 1*1 : 0;

for (var i = lcv; i <= num; i++) {
var elem = name + i;

//This is where the error occurs
var qty = thisForm.elements[elem].value * 1;

if ( isNaN(qty) )
qty = 0;

if (qty < 0) {
alert("You cannot have a negative number");
thisForm.elements[elem].value = 0;
break;
}
val += qty;
}
thisForm.elements[tot].value = val;
}


There is a lot not to like about this function. Is this your own original work,
or did you steal it from a talentless hack?

The program is not indented properly, making it difficult to read. If it is hard
to read, it is even harder to reason about its correctness. Neatness counts.

On line 2, you redefine a parameter as a var. That has been know to cause
Netscape 4 to crash. Is the intention of this line to set num to 5 if it does
not have a value? If so, use the || operator.

On lines 3 and 5, you multiply constants by 1. There is no point in doing that,
so don't. On line 11, you use *1 when you should be using the + operator.

On line 5, lcv is given the value 1 if num doesn't have a value. But line 2
gives num a value. What do you intend? I can't make sense of this.

On line 11, what is thisForm?

The following is less wrong.

function calcIt(name, thisForm, num) {
num = num || 5;
var val = 0;
var tot = name + "T";
var lcv = 0;

for (var i = lcv; i <= num; ++i) {
var elem = name + i;

//This is where the error occurs
var qty = +thisForm.elements[elem].value;

if (isNaN(qty)) {
qty = 0;
}
if (qty < 0) {
alert("You cannot have a negative number");
thisForm.elements[elem].value = 0;
break;
}
val += qty;
}
thisForm.elements[tot].value = val;
}

I have no confidence that it is right, but at least it passes jslint
(http://www.crockford.com/javascript/lint.html).

Jul 20 '05 #4
> There is a lot not to like about this function. Is this your own original work,
or did you steal it from a talentless hack?


Now that's what I call a loaded question...

Holden
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: descds | last post by:
OK im prety much a newbie at PHP but it has me hooked. Im writing a comment system for one of our modules on phpnuke. Its all going very well, well should i say WAS going very well :) Ok all...
7
by: Mark | last post by:
O, woe is me, to have seen what I have seen, see what I see! (That's Shakespeare for those who were wondering what I'm on about) I am "having fun" with cookies. And I wonder if I have...
5
by: Patrick | last post by:
I am trying to download the html page at http://www.dreamteamfc.com/dtfc04/servlet/PostPlayerList?catidx=1 Using the code public class DownloadWebPage { public static void main (String...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
4
by: MAS | last post by:
I want to do what devasp.com has done. They offer their directory via a single-line javascript include. See: http://www.devasp.com/directory.asp You can drop this code onto any web page and...
3
by: Angel Cat | last post by:
Trying to get my jobs to send mail when job fails. Should be easy but it's giving me headache Had a whole slew of issues. Outlook is installed with a n outlook mail profile set up that can...
2
by: Andrew Thompson | last post by:
- NN 4.78 rendering woes, links at far left - I am trying to rework an old site, make it valid html and css, improving the x-browser and 'older browser' compatibility. My efforts so far, have...
0
by: Arun Bhalla | last post by:
I'm having some inconsistency problems with my deployment project ("Setup") and its custom actions ("Installer"). I'm using Visual Studio .NET 2003 (.NET 1.1, no service pack) on Windows XPSP1. ...
1
by: Mark | last post by:
My copy of IE is blocking the following file ( created for test reasons ) with its security message. ( yellow bar at top of browser etc. ) even though ActiveScripting is enabled for the zone. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.