views:

32

answers:

1

I have a form with 26 questions, all of which have radio button groups as an answer choice for the user (it calculates the risk factor for a specific invasive plant species on a given property). At the end, the selected value within each radio group is added up.

What makes it complicated is that the user has the option of filling in the questionnaire for 5 different invasive plant species; in other words, I have 26 rows and 5 columns, and at the end I need to tally up each column separately. I have done this by using getElementsByClassName, and it works like a charm in Firefox, but not in IE. And unfortunately the client I'm doing this for has IE as their user standard. I've tried a number of generic getElementsByClassName functions posted on the web, but they don't seem to work; I get Error on Page all the time.

The function successful in Firefox is like this:

function addSpecies1(frm, resultHolder)
{
var elems = frm.getElementsByClassName('species1'),
calculator = elems.length,
total = 0;
for(var i=0; i<calculator; i++)
if(elems[i].type=='radio' && elems[i].checked  && !isNaN(elems[i].value))
total+=parseFloat(elems[i].value);
resultHolder.value=total;
}

There's probably a very simply answer (I am a rank beginner!) but I've been banging my head against the wall for over a week...

+1  A: 

best and easiest thing to do would be to use jquery. it takes care most cross browser issues. you can do a getElementsByClassName using $('.species1').

If you do not want to use jquery you can use the following code snippet

var inputs = document.getElementsByTagName('INPUT');
var inputlen = inputs.length;
var result = new Array();

for(var i=0;i<inputlen;i++)
{
   if( inputs[i].className == 'species1' && input.checked)
      result.push(inputs[i]);
}

result contains all selected checkboxes with class species1. you can also use the loop to get checked ones

Vinay B R
Or [Prototype](http://prototypejs.org), or [Closure](http://code.google.com/closure/library), or [YUI](http://developer.yahoo.com/yui/), or [MooTools](http://mootools.net/), or [any of several others](http://en.wikipedia.org/wiki/List_of_JavaScript_libraries). jQuery != JavaScript. (But jQuery is great, widely-used, and stable.)
T.J. Crowder
Hi Robert - being such a newbie, I don't know enough about libraries / frameworks to feel comfortable; heck, I'm not even comfortable with this simple javascript function! :-)
Laura Kristiansen
Hi Vinay - I had tried something very much like that, but the result was NaN; when I did some googling, I found out that getElementsByTagName cannot be executed on the input tag. However, I am very grateful for your time and input (ha, ha) and I will try this right now! Thanks again.
Laura Kristiansen
Nope; the getElementsByTagName snippet doesn't work. Error on Page.
Laura Kristiansen
document.getElementsByTagName is upported on IE. are you sure you called it on document and not frm. try using some js debug tool to find out where exactly the exception is thrown. i am sure it is some thing else thats causing the problem
Vinay B R
I have tried debugging (it must indeed be something in that function that is not working, because now it doesn't work in Firefox anymore either), but I decided to stop being frustrated, and have assigned every single radio button (which means 5 x 77 buttons...) their own id. Species 1 is numbered from 1 to 77, Species 2 from 101 to 177, etc. Made an adjustment to the code, and now it works in all browsers:
Laura Kristiansen
function addScoreA(frm, resultHolder){var elems = frm.elements,calculator = frm.elements.length,total = 0;for(var i=0; i<calculator; i++)if(elems[i].type=='radio' resultHolder.value=total;}
Laura Kristiansen
I'm sure this is a cop-out, but it's Friday and I want to get onto my next javascript problem; how to hide certain divs from view if the user clicks a button [sigh]. Thanks for your help Vinay!
Laura Kristiansen