...

View Full Version : checking if an object exists...



ggallen
07-01-2003, 05:24 PM
I'm Dynamically creating an HTML page, which may or maynot
contain some named fields like ENTRY1, ENTRY2. There may
be some skipped numbers, so ENTRY3 may not exist.

My js will need to loop through all possible ENTRY#'s,
but I don't want to get dropped from the function if
I attempt to check a class setting of non-existant field name.

Is there an exists() function in javascript? and how is
it used?

Thanks.

George

Choopernickel
07-01-2003, 05:29 PM
Not quite an exists() function, but here's what I use:


// something may exist
if (something && typeof something != 'undefined') {
// operate on something
}

HTH

Roy Sinclair
07-01-2003, 05:32 PM
Object existance testing is often done like this:

if (document.all)
{// document.all exists}
else
{// document.all doesn't exist}

Purists and the future will likely force you to eventually use the "typeOf" operator:

if (typeOf(document.all)=="undefined")
{// document.all doesn't exist}
else
{document.all exists}

ggallen
07-01-2003, 05:35 PM
Thanks. I'll give the TypeOf a try. That looks like
what I'm after.

George

beetle
07-01-2003, 05:45 PM
Javascript keywords are case-sensitive. It's typeof, not typeOf

Also, since it's technically an operator and not a function, you don't have to use the parentheses

if ( typeof document.all == 'undefiend' )

ggallen
07-01-2003, 05:52 PM
ok....Well doesn't do as expected :)

Here is the function:

function hidebuttons(lineno) {
for (var i = 1 ; i <= 3 ; i++) {
if ( typeOf(eval("document.FORM1.BUTTONS"+i)) == "undefined" ) {
alert("Button"+i+" does not exist!");
} else {
alert("Button"+i+" does exist!");
}
}
}

BUTTONS1 is the ID of a table. Does that make a
difference how I reference it in the typeOf() ?

George

ggallen
07-01-2003, 05:59 PM
OK. I changed the typeOf -> typeof, that stopped
the Object expected error :)

BUT....it can't find any of the table names.

BUTTONS1 BUTTONS2 or BUTTONS3
and I know BUTTONS3 in this case exists.

Can I check the existance of a table the same way
as a field?

George

beetle
07-01-2003, 06:05 PM
try not using eval

if ( typeof document.FORM1.elements['BUTTONS'+i] == "undefined" ) {

ggallen
07-01-2003, 06:20 PM
changed it to the elements[] syntax. Still doesn't find
any of the tables (which only BUTTONS3 exists).

HTML coding for the table is as:
<TD WIDTH=5% ALIGN="CENTER" VALIGN="CENTER">
<TABLE ID="BUTTONS3" BORDER="0" ALIGN="CENTER" VALIGN="CENTER" WIDTH=100%><TR><TD ALIGN="CENTER">
<INPUT TYPE="BUTTON" NAME="LINK3" VALUE="LINK" OnClick="subbmit('LINK3');">
<INPUT TYPE="BUTTON" NAME="MODIFY3" VALUE="UPDATE" OnClick="subbmit('MODIFY3');">
</TD></TR></TABLE>

If tried also:

if typeof getElementByID("BUTTONS"+i) also, but the
getElementByID gives me an object not found error on
the first check (BUTTONS1).

Basically, what I want to do, when a checkbox is checked,
I want a group of buttons to disappear (the disappering part
isn't the problem). So I need to figure out which set of tables
exists, then change their visibility.

Thanks
George

ggallen
07-01-2003, 06:28 PM
if it helps any, this will be used ONLY on IE 5.5 or better.
so cross browser implementation isn't an issue.

Thanks
George

beetle
07-01-2003, 06:31 PM
Oh, these aren't form elements.

Again, case-sensitivity applies

getElementById, not getElementByID

ggallen
07-01-2003, 07:08 PM
type on my part on posting. I did have

getElementById.

Problem is that the getElementById function
has to have a valid Table Name (or rather ID),
and typeof doesn't seem to work for Tables.

My solution for now will be to imbed a HIDDEN
element within the table that I CAN reference
with typeof, ie:

<INPUT TYPE="HIDDEN" NAME="EXISTS3" VALUE="">

Then do a typeof document.FORM1.EXISTS+i If it
exists, then it's table exists :)

If anyone can think of another easy method, I'll
try it.

Thanks
George

ggallen
07-01-2003, 07:13 PM
Using the Imbedded HIDDEN element work
just fine. :D

George

beetle
07-01-2003, 07:32 PM
If the table has an ID, and it indeed does exist in the document, then
if ( typeof document.getElementById( 'tableId' ) == 'undefined' )Should work. Of course, this is typically done with a reference first
var t = document.getElementById( 'tableId' );
if ( typeof t == 'undefined' )

ggallen
07-01-2003, 07:35 PM
In my case, the table may or may not exist. That's
what I'm trying to find out. I'll try anyway returning
the base into a varible and checking the typeof
on the varible.

George

ggallen
07-01-2003, 07:45 PM
well...not exactly what I thought would happen,
and not what I wanted to happen.

I didn't get any errors with:

var test = document.getElementById('BUTTONS'+i);

HOWEVER,

when I did a...

alert("type = "+typeof test) it came back with
"object" regardless of whether BUTTONS1 existed or not.
Since object wasn't undefined, it fell into the EXISTS
section of the if , and then the subsequent attempt
to access the style of the table failed because the
table didn't exist.

George

cheesebagpipe
07-01-2003, 08:57 PM
Just a note to confuse you further: you don't use the typeof operator with document.getElementById() - because it's a method (function), and implements it's own object checking, simply returning null if the (element) object isn't found.

var el;
if (el = document.getElementById('BUTTONS'+i))
....do something with el

beetle
07-01-2003, 09:19 PM
Oh ya - thanks cheese.

I it will return null, which is typeof 'object', so the typeof test fails.

I really need to start getting more sleep :rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum