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
    391
    Thanks
    76
    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
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    if(document.getElementById('your form id')){
    alert('form exists');}

  • Users who have thanked MattF for this post:

    nikko50 (03-23-2010)

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,728
    Thanks
    6
    Thanked 1,015 Times in 988 Posts
    Or
    Code:
    if(document.forms['your form ID']) {
    	alert('form exists');
    }

  • Users who have thanked VIPStephan for this post:

    nikko50 (03-23-2010)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Or:-

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

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 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!

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 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

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,728
    Thanks
    6
    Thanked 1,015 Times in 988 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
    •