JavaScript woes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy

    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,thi sForm,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.elemen ts[elem].value * 1;

    if ( isNaN(qty) )
    qty = 0;

    if (qty < 0) {
    alert("You cannot have a negative number");
    thisForm.elemen ts[elem].value = 0;
    break;
    }
    val += qty;
    }
    thisForm.elemen ts[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="calcI t('draft',this. form);"></td>

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

    <input type="text" name="rep<%=i%> " size=2 maxlength=11 class="Normal"
    onChange="calcI t('rep',this.fo rm);"></td>


    Any help would be appreciated.

    Thanks,
    - Jeremy
  • Lasse Reichstein Nielsen

    #2
    Re: JavaScript woes

    jcapp@belzon.co m (Jeremy) writes:
    [color=blue]
    > by the way, the error is:
    > Error: 'elements[...].value' is null or not an object[/color]
    [color=blue]
    > function calcIt(name,thi sForm,num) {
    > var num = (!num) ? 5 : num;[/color]


    You don't pass a number, so num=5.
    [color=blue]
    > var val = 0 * 1;[/color]

    val = 0;
    [color=blue]
    > var tot = name + "T";
    > var lcv = (!num) ? 1*1 : 0;[/color]

    Num is always "true" at this point, so lcv=1.
    [color=blue]
    > for (var i = lcv; i <= num; i++) {[/color]

    .... so i goes from 1 to 5.
    [color=blue]
    > var elem = name + i;[/color]
    [color=blue]
    > //This is where the error occurs
    > var qty = thisForm.elemen ts[elem].value * 1;[/color]

    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.
    [color=blue]
    > And here is the HTML that's calling this function:[/color]

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Jeremy Capp

      #3
      Re: JavaScript woes

      Thanks for the quick reply.
      [color=blue]
      > No. That's not the HTML. It looks like some server-side > script, but[/color]
      it is not the HTML that is sent to the[color=blue]
      > server, so it is not the HTML that fails ... so we can't > see what's[/color]
      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...

      [color=blue]
      > My guess is that you don't have draft1 to draft5 in the > page, but I[/color]
      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!

      Comment

      • Douglas Crockford

        #4
        Re: JavaScript woes

        > I have this javascript code that decided that it didn't want to work[color=blue]
        > 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:[/color]
        [color=blue]
        > function calcIt(name,thi sForm,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.elemen ts[elem].value * 1;
        >
        > if ( isNaN(qty) )
        > qty = 0;
        >
        > if (qty < 0) {
        > alert("You cannot have a negative number");
        > thisForm.elemen ts[elem].value = 0;
        > break;
        > }
        > val += qty;
        > }
        > thisForm.elemen ts[tot].value = val;
        > }[/color]

        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.eleme nts[elem].value;

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

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

        Comment

        • Holden Caulfield

          #5
          Re: JavaScript woes

          > There is a lot not to like about this function. Is this your own original work,[color=blue]
          > or did you steal it from a talentless hack?[/color]

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

          Holden

          Comment

          Working...