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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I'm not using a Javascript Object correctly

    Below is a code extract with a problem. I guess I'm just not thinking rightly about JS objects.

    I get the following error message:
    'x[...].text' is null or not an object
    There is a single OPTION with a value in the TEXT property that is NOT null. So it's "not an object"??

    The error msg is pointing to the for() statement below

    Code:
    function InsertNewKeyword(theform)	{  
    // "theform" contains a SELECT object named "newList"
    
       x = theform.newList.options;
    
       for ( var i=0; i>= (x.length-1); i++) {
          debugstring += "\t Text[" + i + "] = " + x[i].text + "\r";
       }
       alert (debugstring);
    Last edited by RadarBob; 07-03-2002 at 03:14 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't add to a string (+=) if it doesn't exist. Try putting:

    debugstring = "";

    before the loop.

    Also:

    x = document.formname.selectname

    to get the value of an option:

    tmp = x[2].text
    Last edited by x_goose_x; 07-03-2002 at 04:03 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by x_goose_x
    [B]You can't add to a string (+=) if it doesn't exist
    Code:
    var debugstring = new String ("")
    --> was done earlier in the function; not shown.

    x = document.formname.selectname

    to get the value of an option:

    tmp = x[2].text
    Why?

    The following code inside the function displays the proper value with no errors (just prior to getting the the for() statement wherein the error is flagged):
    Code:
    alert (x[0].text)

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    for ( var i=0; i<x.length; i++) {

    ...or

    for ( var i=0; i<= (x.length-1); i++) { //pointless but OK

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's consistant with the style of the pre-existing code.


    "Focus."
    -- Mr. Miaggi

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree with adios... why make it more confusing?

    To heck with "being consistent with the style of the previous code", unless there's a specific reason you want to put in unnecessary stuff.

    Of course it works, but it just puts a couple of extra steps in to do the same thing...

    As for your other question... can you post the whole script?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    uhm, this loop:

    for ( var i=0; i>= (x.length-1); i++) {
    }

    should never start... you shouldn't even get the error.

    i=0

    go on as long as i is > or equals x.length-1

    never starts: i is not > since the beginning

    the correct form is:

    for(var i=0; i<x.length; i++){

    }

    So before getting deeper in this: hey, how's possible that loop starts??
    Try this:

    var x=new Array(1,2,3)
    for ( var i=0; i>= (x.length-1); i++) {
    alert(x[i])
    }

    never starts.
    Now, try this dire one:

    var x=new Array()
    for ( var i=0; i>= (x.length-1); i++) {
    if(!confirm(x[i])){break;}
    }

    it starts and NEVER stops. In fact: array has length=0, so:

    i=0
    i>=length-1 (length-1= ... -1)

    so i is > length: the loop starts.
    It now increments by 1
    i++=1
    still higher than length

    i++=2
    still higher then length

    i++=3
    still higher than length

    ...goes on infinitely

    In simplier words, whatever the problem, first thing I really suggest to fix is the way that loop is designed, then refer to what x_goose_x wrote. You' ll see it works.
    ciao!
    Alberto http://www.unitedscripters.com/

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    post deleted by whammy
    Last edited by whammy; 07-05-2002 at 11:54 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    adios:

    for ( var i=0; i<x.length; i++) {
    TrueLies:

    the correct form is:
    for(var i=0; i<x.length; i++){

    }
    Thanks for clearing that up.

    uhm, this loop:
    for ( var i=0; i>= (x.length-1); i++) {
    }

    should never start... you shouldn't even get the error.
    So if x.length=1 - as it does with one OPTION - x.length-1 will never equal zero? Interesting. The conditional, of course, is backwards, so x[i].text, on the second iteration, will evaluate to x[1].text - which doesn't exist. It's a simple runtime error. Tell me what I missed, whammy....

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nothing... LOL!

    Geez... I look at code all day - and I guess I get a bit burned out on it at times (and I always miss the simple math stuff when I didn't do it )... *doh*
    Last edited by whammy; 07-05-2002 at 11:55 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #11
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Adios hey how comes you felt called in by *me*? I mean, what's your problem that you take the initiative to quote me directly for matters of no importance?

    I was replying to whammy *obviously*. I mean that a post like yours saying:

    ----quote

    adios:
    --------------------------------------------------------------------------------
    for ( var i=0; i<x.length; i++) {
    --------------------------------------------------------------------------------
    TrueLies:
    --------------------------------------------------------------------------------
    the correct form is:
    for(var i=0; i<x.length; i++){

    }
    --------------------------------------------------------------------------------
    Thanks for clearing that up.
    ----unquote


    Is a typical, *remarkable* example of a *gratuitously provocative* post (stress on gratuitous). I was replying to whammy, correct? Can't I stress my point when we get a request of help, even if it replicates a couple of lines of already said things, in order to add something new to the thread?:

    --quote
    Now, try this dire one:

    var x=new Array()
    for ( var i=0; i>= (x.length-1); i++) {
    if(!confirm(x[i])){break;}
    }
    --unquote

    It is obvious that you can have an x object whose length is 1: who doubted it? you can have objects whose length is whichever number: indeed, not a good reason to reccomend that type of loop LOL! - but here we agree (arguably).

    What I don't understand is: everybody has a track here, and I never found that yours was the track of a person who is not helpful: I always respected you, never addressed you rejecting the opportunity of your replies to members, never sent a rebuttal quoting you, and certainly I wasn't prowling for an occasion: I don't have hidden agendas.

    I'm surprised that you got the idea I'm a person who can be mocked at by you gratuitously and without any provocation.

    For me the matter is already over, but I wonder what we can do if one sends a helpful reply and can even get a rebuttal by folks who sent their replies three posts before and none the less argue the reply was meant for *them*, even without addressing them *at all*!

    Mah.
    ciao
    Alberto http://www.unitedscripters.com/

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The Final Solution...

    The for loop was the problem. Here's what I coded:
    Code:
     for (var i=0; i >=(x.length-1); i++) {...}
    Here's what worked:
    Code:
     for (var i=0; i <=(x.length-1); i++) {...}
    My interpretation of what happened: The loop did in fact execute (since the "=" was there). However, "i" was not initialized properly for whatever reason, thus "x[i]" was not "built" and therefore was "null". x[0] did have a value, I could see it on some debugging output prior to the loop. I also think the loop actually executed because when I took out the "x[i]" reference, I did not get that original error message and I was apparently stuck in an infinite loop.

    BTW I'm running on Win 98, IE 5.5
    Last edited by RadarBob; 07-08-2002 at 02:41 PM.


  •  

    Posting Permissions

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