SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2004
    Location
    Vancouver
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking existence of form element

    Hi JS gurus,

    Newbie here! How do I check the existence of FORM elements using getElementById ?

    I have two forms. Form A with element1 and Form B without element1. I need to perform certain functions if element1 exists on two of the forms which would share same function : somefunc(in_element)

    Tried the following code and it is not working, it always go into element exist.
    function somefunc(in_element)
    if (document.getElementById(in_element)) {
    // element exist on form
    do something....;
    }
    else {
    // element does not exist on form
    do something else ;
    }

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    You were really close! Here's a way that works:
    Code:
    if (typeof document.getElementById(in_element) == 'object')
    {
      //element exists
    }
    else 
    {
      //element doesn't exist
    }
    Hope this helps!


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
  •