#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    137
    Rep Power
    11

    IE: getting value from radio button array


    Hi,

    I have some code that works in Fox but not IE (big surprise there!) I was hoping someone knew what I'm referencing wrong. Here is the code:

    Code:
    for(var i in document.forms[form].elements['customReason']) {
    		alert(document.forms[form].elements['customReason'][i].value);
    		if(document.forms[form].elements['customReason'][i].checked == true)
    			selected = true;
    			break;
    		}
    The portions in bold, above, are just returning 'undefined' rather than the 'value' or the 'checked' boolean. I assume I'm referencing the radio button array incorrectly for IE, but I'm not sure what the right method is. Any assistance?

    Edit to add:
    Right after entering the loop, I put in: alert(i); just to check the value. In FireFox, it correctly alerts the index of array, but in IE, the alert just shows 'length' - I'm def. confused now!
    Last edited by Nutopia; June 23rd, 2009 at 11:14 AM.
    No trees were hurt in the sending of this message, however, a large number of electrons were terribly inconvenienced.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    137
    Rep Power
    11
    I just rewrote my loop. I guess IE doesn't like for...in. Anyway, here's my work around:

    Code:
    		for(var i = 0; i < document.forms[form].elements['customReason'].length; i++) {
    			if(document.forms[form].elements['customReason'][i].checked == true) {
    				selected = true;
    				break;
    			}
    		}
    No trees were hurt in the sending of this message, however, a large number of electrons were terribly inconvenienced.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Thank you!


    Originally Posted by Nutopia
    I just rewrote my loop. I guess IE doesn't like for...in. Anyway, here's my work around:

    Code:
    		for(var i = 0; i < document.forms[form].elements['customReason'].length; i++) {
    			if(document.forms[form].elements['customReason'][i].checked == true) {
    				selected = true;
    				break;
    			}
    		}
    I had the same problem an solved it with your workaround. Thank you for posting your solution, that should not have been necessary if IE worked properly....
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Welcome to DevShed Forums, yvesined.

    The thing is that it's not an array. It's an "HTMLCollection", which can mostly be used like an array, but as you see it's not quite the same.

    In general, it's best to only use for-in loops when looping through properties of an object.

    By the way, you don't need the "== true" in the condition.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo