SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Determining the existance of an element

    Hi !

    I was wondering if there was any method for determining the existance (or no existance) of a given DOM element using its Id.

    My problem is the following :

    I have some ajax stuff that does some crazy things to my original document well... errr... such as simply adding some new layers with a unique Id.

    What I wanted to do, from another JS function that is triggered later on, was to check if theses layers were existing or not before populating them.

    For example something like :

    if (document.getElementById("blah").exists){

    //populate the thingie !

    }

    Thank you all for helping !

    Cheers
    Alex

  2. #2
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry,

    It seems that it is as simple as
    if (document.getElementById("blah")){
    alert("blah exists");
    }

    Let me know if there are some traps I should not fall into while using this.

    Thx

  3. #3
    The Omnipresent [ArcanE]'s Avatar
    Join Date
    Mar 2005
    Location
    Belgium
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only trap i could think of is that getElementById isn't supported by some older browsers so you could get unexpected results since in these browsers this will almost always result false or give an error.

    you can use if(!document.getElementById) return; in your functions to prevent the script from being executed when this method isn't available.
    Webdevelopment : Skyrocket Concepts Inventis Web Architects
    Ain't got time for the future or the past.
    Live for the moment, make it last.

  4. #4
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I'm using prototype at the moment. I tried using if($"blah"){} and it seems to work fine and as far as i'm aware of, prototype has some built in funcitons that ensure cross browser compatibility. I just hope it will work in older browsers the same way

  5. #5
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing to watch out for is trying to run your script before the DOM has loaded.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById will return null if element is not found, so you can simply
    Code:
    var oNode = document.getElementById("myElement");
    
    if(oNode != null)
    {
        // Do some magic
    }
    Null is not true, so you can also just do if(oNode)


Bookmarks

Posting Permissions

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