• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Javascript Radio button by name

I'm trying to get a radio button to check using javascript but don't know how to assign the checked value. Default value is taken from a hidden form element.

function setdefault(id){

                var inputid="input" + id;
                var defaultvalueid="defaultvalue" + id;
               
                if(document.getElementById(defaultvalueid).value==0){
                                document.setup.inputid[0].checked=true;
                }else{
                                document.setup.inputid[1].checked=true;
                }
}

FF error console message
Error: document.setup.inputid has no properties
0
bergstrom_davin
Asked:
bergstrom_davin
  • 5
  • 2
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
Like this:


function setdefault(id){
  var theForm = document.setup; // if setup is the form name
//or:
  var theForm = document.forms[0];
  var inputid="input" + id;
  var defaultvalueid="defaultvalue" + id;
               
  if(theForm[defaultvalueid].value==0){
      theForm[inputid][0].checked=true;
  }else{
      theForm[inputid][1].checked=true;
  }
}


0
 
ZvonkoSystems architectCommented:
Or shorter:

Like this:


function setdefault(id){
  var theForm = document.forms[0];
  if(theForm["defaultvalue" + id].value==0){
      theForm["input" + id][0].checked=true;
  }else{
      theForm["input" + id][1].checked=true;
  }
}

0
 
Michel PlungjanIT ExpertCommented:
Points to Zvonko, but


function setdefault(id){
  var fld = document.setup["input" + id];
   fld[(fld.form["defaultvalue" + id].value==0)?0:1].click();
}

;)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
bergstrom_davinAuthor Commented:
Thank you both.
0
 
ZvonkoSystems architectCommented:
If we both helped you then split points would be appropriate choice. Should we ask for question reopen so you can do the split?
0
 
Michel PlungjanIT ExpertCommented:
Nah, not needed.
I just wanted to post a not-readable shorter version :)
0
 
ZvonkoSystems architectCommented:
I needed some time to see where fld is coming from because I focused on 0:1  ;-)
0
 
bergstrom_davinAuthor Commented:
I would have but per mplungjan's request I did not.
0
 
ZvonkoSystems architectCommented:
Ok, thanks for care.

See you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now