?
Solved

Javascript Radio button by name

Posted on 2007-10-15
9
Medium Priority
?
226 Views
Last Modified: 2011-09-20
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
Comment
Question by:bergstrom_davin
  • 5
  • 2
  • 2
9 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 20082173
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
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 20082183
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20085137
Points to Zvonko, but


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

;)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:bergstrom_davin
ID: 20085757
Thank you both.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20088036
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20088046
Nah, not needed.
I just wanted to post a not-readable shorter version :)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20088095
I needed some time to see where fld is coming from because I focused on 0:1  ;-)
0
 
LVL 1

Author Comment

by:bergstrom_davin
ID: 20088638
I would have but per mplungjan's request I did not.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20088963
Ok, thanks for care.

See you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question