#1
  1. a lazy guy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Hong Kong
    Posts
    99
    Rep Power
    13

    Javascript: check a form object is exist or not


    Hello,

    How do you check a form object is exist or not in javascript?

    eg.
    in html(A):

    <html>
    <form name="myForm">
    input: <input name="myBox" type="text">
    </form>
    </html>

    in html(B):

    <html>
    <form name="myForm">
    </form>
    </html>


    In the HTML(B), the input box named 'myBox' is not exist.
    How do I check its existence in javascript?

    the code belows does not work
    if (document.MyForm.myBox)
    alert("exist!");
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    You get exactly one undefined property without JavaScript throwing an error - so
    if (document.MyForm.myBox)
    should work fine, so long as there's a MyForm to find. My guess is: you're running that in the <head>er, before the form has been created. There's nothing but a document object at that point so you'll definately get an error when the script engine hits MyForm. Put it in a function & call onload, or stick in a JS block below the form.

IMN logo majestic logo threadwatch logo seochat tools logo