Topic: Help with DOM walking in MSIE...

I'm trying to help a colleague with a little bit of JS to work around a frameworks limitations (don't ask, not RoR).

I have multiple groups of radio buttons (different names) that need to be treated like a single virtual group -- ie exclusive selection across all the groups, while the groups have different names.

My first approach was something like:

function enforce_unique_selection2(clicked) {
var buttons = new Array("foo1", "foo2", "foo3", "foo4", "foo5",
                        "bar1", "bar2", "bar3", "bar4", "bar5",
                        "baz1", "baz2", "baz3", "baz4", "baz5", 
                        "qux1", "qux2", "qux3", "qux4", "qux5",
                        "quux1","quux2","quux3","quux4","quux5")
for (x in buttons) {
  if (buttons[x]!=clicked) {
    ele = document.getElementById(buttons[x])
    ele.checked=false;
  }
}
}

This works in both MSIE and Firefox; however generating the hard-coded array isn't really an option either.

So next I tried to do some DOM based inspection of the form:

function enforce_unique_selection(clicked) {
  clickedEle = document.getElementById(clicked)
  form = document.getElementById('theform')
  // formItems= document.getElementsByTagName('input')
  formItems = form.elements
  for (x in formItems) {
    testEle = formItems[x]
    if ((testEle!=clickedEle) && (testEle!=null) && (testEle.type=="radio")) {
      testEle.checked=false;
    }
  }
}

This works in Firefox, but not IE.  (And IE is the primary/only target for this app).  The commented out alternative using getElementsByTagName didn't work in either browser.

Ideally I'd also want to do some form of a regexp/partial match on the elements name as it will have a known prefix string that will let me avoid messing with the other radio buttons on the form that aren't part of the "meta-group".

Can anyone offer any advice?

Thanks

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Help with DOM walking in MSIE...

You are missing some semi-colon line endings. Perhaps that is what the problem is with IE not working?

Railscasts - Free Ruby on Rails Screencasts

Re: Help with DOM walking in MSIE...

Added semi-colons, no change in behavoir.  (I had thought I had read they were optional in JS...)

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Help with DOM walking in MSIE...

Oh, I guess they are. Have I mentioned I hate JavaScript? I think I'm going to avoid this forum. wink

Seriously though, I think the problem is with the loop. Try changing it to this:

for (var x=0; x<formItems.length; x++) {

Railscasts - Free Ruby on Rails Screencasts

Re: Help with DOM walking in MSIE...

Interesting... all the tutorials said that both forms of for existed, but that they were basically the same....  They're not.  The old-school 'c' loop appears to work, while the newer foreach style loop doesn't.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.