PDA

View Full Version : Java Script, radio group value



agcssautomotive.com
02-06-2007, 01:27 PM
Well, I have a form, which offers two radio buttons to search either osCommerce store or the site.

Long story short:


document.searchform.action= document.searchform.searchGroup1.value;


Gives "undefined" in the URL bar of IE.. Dont get confused here, the above code does nothing, Ill have if statements to handle action based on selection. But it didnt work, so this is how I debug it.


Why does the above code give undefined?


I was expecting "rb1_value" instead of "undefined".

areidmtm
02-06-2007, 01:49 PM
I believe that radio groups are put into an array. SO it would be like this,

document.searchform.searchGroup1[0].value;
document.searchform.searchGroup1[1].value;

You'd have to do some checking to see if it's checked.


var groupArray = document.searchform.searchGroup1;

for (i=0; i<groupArray.length; i++) {
if (groupArray[i].checked === true) {
document.searchform.action=document.searchform.sea rchGroup1[i].value;
}
}

Something like that. This was just off the top of my head

agcssautomotive.com
02-06-2007, 02:13 PM
Thanks guy. That really helped me. :p


I now have


if(document.radiogroup1[0].checked==true)
{
}
else
{
}


Since there is only two radio buttons, one for searching store, the other for searching site, it works perfectly.