Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-01-2003, 04:24 PM   PM User | #1
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
checking if an object exists...

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
ggallen is offline   Reply With Quote
Old 07-01-2003, 04:29 PM   PM User | #2
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
Not quite an exists() function, but here's what I use:

Code:
// something may exist
if (something && typeof something != 'undefined') {
    // operate on something
}
HTH
Choopernickel is offline   Reply With Quote
Old 07-01-2003, 04:32 PM   PM User | #3
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
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}
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.
Roy Sinclair is offline   Reply With Quote
Old 07-01-2003, 04:35 PM   PM User | #4
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
Thanks. I'll give the TypeOf a try. That looks like
what I'm after.

George
ggallen is offline   Reply With Quote
Old 07-01-2003, 04:45 PM   PM User | #5
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
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' )
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 07-01-2003, 04:52 PM   PM User | #6
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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 is offline   Reply With Quote
Old 07-01-2003, 04:59 PM   PM User | #7
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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
ggallen is offline   Reply With Quote
Old 07-01-2003, 05:05 PM   PM User | #8
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
try not using eval

if ( typeof document.FORM1.elements['BUTTONS'+i] == "undefined" ) {
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 07-01-2003, 05:20 PM   PM User | #9
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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 is offline   Reply With Quote
Old 07-01-2003, 05:28 PM   PM User | #10
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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
ggallen is offline   Reply With Quote
Old 07-01-2003, 05:31 PM   PM User | #11
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Oh, these aren't form elements.

Again, case-sensitivity applies

getElementById, not getElementByID
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 07-01-2003, 06:08 PM   PM User | #12
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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 is offline   Reply With Quote
Old 07-01-2003, 06:13 PM   PM User | #13
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
Using the Imbedded HIDDEN element work
just fine.

George
ggallen is offline   Reply With Quote
Old 07-01-2003, 06:32 PM   PM User | #14
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
If the table has an ID, and it indeed does exist in the document, then
Code:
if ( typeof document.getElementById( 'tableId' ) == 'undefined' )
Should work. Of course, this is typically done with a reference first
Code:
var t = document.getElementById( 'tableId' );
if ( typeof t == 'undefined' )
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 07-01-2003, 06:35 PM   PM User | #15
ggallen
Regular Coder

 
Join Date: Jul 2003
Location: NJ - #29 if you must know
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
ggallen is an unknown quantity at this point
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 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.