SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to tell if a form object exists?

    I have a form, that depending on some conditions MAY have a certain drop down box in it.

    I would like to have a javascript function that checks to see IF the drop down box exists? ie: Does the javascript object exist?

    Sort of like this:

    function resetMenu1(){
    //Test to see if dropdown exists.
    if exist(document.menu.file.value) {
    document.menu.file.value = "Yes!";
    } else {
    alert("Box does not exist.")
    }
    }

    Anyone know how to do this? It's the "if exist" part I don't know how to do.

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (document.formname.elementname)
    {

    }
    else
    {

    }

    That will tell if it exists or not
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    JavaScript Guru (Big Ego) Arielladog's Avatar
    Join Date
    Jul 1999
    Location
    SC, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys,

    sometimes I find that this works better for detecting if something exists or not:

    if(typeof document.formname.elementname != "undefined"){
    // exists
    }

    aDog

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Birmingham, UK
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey there, looking at your script you seem to be looking to see if the drop down has any values in it...if this is so...then you can just check to see if there are any values by passing the values up into a script

    <Script language="JavaScript">
    function check(){
    if(document.form.drop.value==""){
    alert("yup...the bugger exists")
    }
    else{
    alert("nope")
    }
    }
    </Script>

    what this basically does is, when the page loads it checks to see what the first field in the drop down is...if it doesnt equal "" then it exists...if it does equal "" then it has no values and therefore doesnot exist...
    The only problem with this is...if you happen to have for some freak reason a field in the drop down that doesnt equal anythin but still has other fields then it will still think it doesnt exist...

    anythoer way of doing it i suppose is... i dont know the actuall code but will work something like this.

    <Script language="JavaScript">
    function check(){
    var hasFields = false
    var id = new Array()
    //loop through the fields
    for(i=0;i<=document.form.drop.id.length;i++){
    id[i] = document.form.drop.value
    // if i is >= than or equal to 1..it exists
    if(i>=0){
    alert("yup...the bugger exists")
    }
    else{
    alert("nope")
    }
    }
    }
    </Script>

    hope it helped


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
  •