SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking if form element exists

    Hi All

    I am having trouble checking if a form element exists.

    I have a listbox array, with an ID send[0] thru to send[4] (ie. 5 different list boxes).

    However sometimes for whatever reason a list box may not exist, for example I may only have:
    send[0]
    send[2]
    send[3]
    send[4]

    send[1] isn't required.

    In a javascript loop from 0-4 what is the best/ most browser compatable way of checking if the listbox exists.

    I have come up with 2 ways, 1 works in Firefox, and one in IE
    Respectively:
    if (document.getElementById('send[' + i + ']')) {
    or if (document.getElementById('send[' + i + ']').options != null) {

    But I can't work out a way of doing it that works across all browsers.

    Any suggestions would be great.

    Thanks

  2. #2
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are your select boxes actually tagged id="send[0]" or are each individual elements stored in JavaScript array?
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    each select box is tagged id="send[x]" where x is 0-4

  4. #4
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't see why this won't work in IE and FF.
    Code:
     if (document.getElementById('send[' + i + ']')) {
    Thats the Standard approach to object detection.
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hhhmmm, I get javascript errors with that approach in IE.
    Object not defined I think it was.

  6. #6
    Also available in Large Si's Avatar
    Join Date
    Sep 2002
    Location
    Walsall, UK
    Posts
    1,911
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might be worth trying to reverse the logic...
    Code:
    if (!document.getElementById('send[' + i + ']')) {
      // if object doesn't exist, do nothing.
      return false;
    } else {
      // object exists, continue...
      ...
    }
    Si
    Are you a Photoshop Jedi Master? Prove it!

    Is funky house your bag? You'll love this!

    Voice
    , eyes, ears, body and hands.


  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still get an Object Expected error doing that approach too.

    The error is happening at the element that doesn't exist , eg:
    send[0]
    send[2]
    send[3]
    send[4]

    The missing send[1] check returns the error as the object doesn't exist.

    This is the code I have:

    PHP Code:
    if (!document.getElementById('send[' ']')) {
    } else {


  8. #8
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No other ideas?

  9. #9
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still can't get this to work, any further ideas?

  10. #10
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, my solution to the problem is:
    previously where the blanks where in the array of send[] list boxes I had a hidden field by the same name, send[]. However the hidden field didn't have an ID, thus why the:
    Code:
    if (!document.getElementById('send[' + i + ']')) {
    } else {
    }
    solution should have worked..though it didn't.

    I have now added an ID to the hidden field, and used the following javascript to check if the form element is a listbox or not:
    Code:
    if (document.getElementById('send[' + i + ']').getAttribute("type") != "hidden") {
    }
    Is this solution cross browser compatable?

  11. #11
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about getting an array of lists by using document.getElementsByTagName('select'), then looping through the array to check id values?


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
  •