View Full Version : I'm not using a Javascript Object correctly
RadarBob
07-03-2002, 03:11 PM
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
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);
x_goose_x
07-03-2002, 03:53 PM
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
RadarBob
07-03-2002, 05:18 PM
Originally posted by x_goose_x
[B]You can't add to a string (+=) if it doesn't exist
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):
alert (x[0].text)
adios
07-03-2002, 07:07 PM
for ( var i=0; i<x.length; i++) {
...or
for ( var i=0; i<= (x.length-1); i++) { //pointless but OK
RadarBob
07-03-2002, 09:17 PM
It's consistant with the style of the pre-existing code.
"Focus."
-- Mr. Miaggi
whammy
07-04-2002, 01:29 AM
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? :)
TrueLies
07-05-2002, 12:50 AM
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!
whammy
07-05-2002, 10:50 PM
post deleted by whammy ;)
adios
07-05-2002, 11:47 PM
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....:confused:
whammy
07-05-2002, 11:49 PM
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*
TrueLies
07-06-2002, 10:24 AM
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
RadarBob
07-08-2002, 02:37 PM
The Final Solution...
The for loop was the problem. Here's what I coded:
for (var i=0; i >=(x.length-1); i++) {...}
Here's what worked:
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
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.