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 5 of 5
  1. #1
    Regular Coder vw98034's Avatar
    Join Date
    Feb 2004
    Posts
    196
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Nonexistent element, sometimes

    I am writing Javascript code of user entry data validation on a dynamic page. One element/field of the form can be nonexistent in a situation. Currently, I put any Javascript code related with the field on the bottom so that other code work regardless whether the element/field exists or not. Is a better way of dealing such situation?

    Thanks for your input.

  • #2
    New to the CF scene
    Join Date
    Mar 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your direction. This I find very helpful.

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    dont know how you identify or address the element but

    in a form

    if (form.elementname){

    }

    ir if id

    if (document.getElementById(elementIDname)){


    }

    or even

    window['elementIDname']
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    Regular Coder vw98034's Avatar
    Join Date
    Feb 2004
    Posts
    196
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vwphillips
    dont know how you identify or address the element but

    in a form

    if (form.elementname){

    }

    ir if id

    if (document.getElementById(elementIDname)){


    }

    or even

    window['elementIDname']
    Thanks.

    I have an element of the form defined with a name and an ID. In the Javacript, an element is referred as

    document.forms['myForm'].elements['subject']

    I could check whether it is null or not with if condition statement.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can also use:
    Code:
    var el = document.forms['myForm'].elements['subject'];
    if (typeof el != "undefined"){
       //el exists
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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