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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not quite an exists() function, but here's what I use:

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

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I'll give the TypeOf a try. That looks like
    what I'm after.

    George

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #9
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #12
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using the Imbedded HIDDEN element work
    just fine.

    George

  • #14
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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”

  • #15
    Regular Coder
    Join Date
    Jul 2003
    Location
    NJ - #29 if you must know
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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