Web Analytics Made Easy -
StatCounter I'm not using a Javascript Object correctly - CodingForum

Announcement

Collapse
No announcement yet.

I'm not using a Javascript Object correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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; Jul 3, 2002, 10:14 AM.

  • #2
    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; Jul 3, 2002, 11:03 AM.

    Comment


    • #3
      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)

      Comment


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

        ...or

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

        Comment


        • #5
          It's consistant with the style of the pre-existing code.


          "Focus."
          -- Mr. Miaggi

          Comment


          • #6
            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. ;)

            Comment


            • #7
              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/

              Comment


              • #8
                post deleted by whammy
                Last edited by whammy; Jul 5, 2002, 06:54 PM.
                Former ASP Forum Moderator - I'm back!

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

                Comment


                • #9
                  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....

                  Comment


                  • #10
                    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; Jul 5, 2002, 06:55 PM.
                    Former ASP Forum Moderator - I'm back!

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

                    Comment


                    • #11
                      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/

                      Comment


                      • #12
                        The Final Solution...

                        The for loop was the problem. Here's what I coded:
                        Code:
                         for (var i=0; i [COLOR=crimson]>=[/COLOR](x.length-1); i++) {...}
                        Here's what worked:
                        Code:
                         for (var i=0; i [COLOR=crimson]<=[/COLOR](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; Jul 8, 2002, 09:41 AM.

                        Comment

                        Working...
                        X