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.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    404
    Thanks
    78
    Thanked 0 Times in 0 Posts

    check if form exists

    Can I do something like the below to see if a form exists in a document.

    Code:
    if(document.theForm){
    alert('form exists');
    }

  2. #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,322
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    if(document.getElementById('your form id')){
    alert('form exists');}

  3. Users who have thanked MattF for this post:

    nikko50 (03-23-2010)

  4. #3
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,873
    Thanks
    6
    Thanked 1,299 Times in 1,269 Posts
    Or
    Code:
    if(document.forms['your form ID']) {
    	alert('form exists');
    }

  5. Users who have thanked VIPStephan for this post:

    nikko50 (03-23-2010)

  6. #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,466
    Thanks
    217
    Thanked 2,699 Times in 2,675 Posts
    Or:-

    Code:
    <form name = "myform">
    </form>
    
    <script type = "text/javascript">
    if(document.myform) {alert ('Form exists')}
    else {alert ("No such form exists")}
    </script>

  7. #5
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,510
    Thanks
    22
    Thanked 567 Times in 560 Posts
    Quote Originally Posted by VIPStephan View Post
    Or
    Code:
    if(document.forms['your form ID']) {
    	alert('form exists');
    }
    not sure, whether this works in FF & Co.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  8. #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,466
    Thanks
    217
    Thanked 2,699 Times in 2,675 Posts
    Quote Originally Posted by Dormilich View Post
    not sure, whether this works in FF & Co.
    It works fine for me in FF. Not hard to find out!

  9. #7
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,510
    Thanks
    22
    Thanked 567 Times in 560 Posts
    with IDs I’d always use getElementById(). just makes more sense.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  10. #8
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,873
    Thanks
    6
    Thanked 1,299 Times in 1,269 Posts
    Whatever. But you can also use this to check if there are any forms like:
    Code:
    if(document.forms[0]) {
    	alert('form exists');
    }


 

Posting Permissions

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