SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting strange JS error in IE 6 and IE 7 ..please suggest

    IE 6 and IE 7 are throwing errors at me. The JS error I get is
    'elements[...].value' is null or Not an object

    Code:0

    What could it be ? Please help me


    CODE
    var bform = document.forms['billing'];

    var postStr = window.location.search.substring(1);

    for (var m in bform.elements) {
    if ((bform.elements[m].value != null) && (bform.elements[m].value != null))
    {postStr += "&" + bform.elements[m].name + "=" + escape(bform.elements[m].value);


    }

    req.send(postStr);

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You shouldn't be using a for...in loop since the elements collection is not an object literal, it is an array.

    Try using a normal for loop. Also, why are you checking (bform.elements[m].value != null) twice?

    Personally I have practically no experience using that syntax for accessing form controls. I prefer to use DOM methods like getElementsByTagName, I feel like I have more control that way.

  3. #3
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the reason it didn't work is because you used "for... in" which gets everything in the elements HTML collection including some properties like scrollHeight, etc... (I could be wrong, could someone verify?)

    Try replacing

    Code:
    for (var m in bform.elements) {}
    with

    Code:
    for (var m=0; m<bform.elements.length; m++) {}
    Worked for me.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, the for...in gets lots of other things. For example on a page with one form:
    Code:
    <select id="perpage" name="perpage">FORM
     null
     1
     <div id="bar">
     ["\n      ", fieldset, "\n    "]
     "\n      "
     "\n    "
     "\n    "
     "\n  "
     [Attr localName=action nodeValue=misc/1/ nodeName=action, Attr localName=method nodeValue=post nodeName=method]
     Document misc
     insertBefore()
     replaceChild()
     removeChild()
     appendChild()
     hasChildNodes()
     cloneNode()
     normalize()
     isSupported()
     null
     null
    FORM
     hasAttributes()
     1
     2
     3
     4
     5
     6
     7
     8
     9
     10
     11
     12
    FORM
     getAttribute()
     setAttribute()
     removeAttribute()
     getAttributeNode()
     setAttributeNode()
     removeAttributeNode()
     getElementsByTagName()
     getAttributeNS()
     setAttributeNS()
     removeAttributeNS()
     getAttributeNodeNS()
     setAttributeNodeNS()
     getElementsByTagNameNS()
     hasAttribute()
     hasAttributeNS()
    
    
    
    
    
     [fieldset, select#perpage]
     2
    
    
    
    post
    
     submit()
     reset()
    
     0
     12
     971
     0
     <div id="bar">
           <fieldset>         <legend>Settings</legend>         <label for="perpage">Thumbs per page:</label>         <select name="perpage" id="perpage">           <option value="10">10</option>           <option value="15">15</option>           <option value="20" selected="selected">20</option>           <option value="25">25</option>           <option value="30">30</option>           <option value="40">40</option>           <option value="50">50</option>           <option value="80">80</option>         <option value="custom">Custom ...</option></select>         <input src="img/right.png" alt="Go" type="image">       </fieldset>     
     0
     0
     0
     971
     0
     971
     -1
     blur()
     focus()
     false
     []
     removeEventListener()
     dispatchEvent()
     compareDocumentPosition()
                    Settings         Thumbs per page:                    10           15           20           25           30           40           50           80         Custom ...                     
     isSameNode()
     lookupPrefix()
     isDefaultNamespace()
     lookupNamespaceURI()
     isEqualNode()
     getFeature()
     setUserData()
     getUserData()
     1
     2
     4
     8
     16
     32


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
  •