Solved

How do i test if a radio button collection exists?

Posted on 2004-04-27
5
849 Views
Last Modified: 2012-06-27
Hello, I have the below code:

            for(i=0; i<document.form1.PackageID.length; i++) {
                  if(document.form1.PackageID[i].checked==true) {
                        if((document.form1.PackageID[i].value)!='') {
                              PackageSelected = 1;
                        }
                  }
            }

I have XML dynamically generating a run of <input type=radio>'s on the fly with the NAME="PackageID", But if this FOR loop attempts to test before the radio's exist I get an error:
document.form1.PackageID.length is null or not an object

Can i test like an isExist or isNull in javascript? If so, is that what i use?
Thanks in advance...
0
Comment
Question by:Darren_Fryer
  • 3
5 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 10936113
Hi Darren_Fryer,

This link may help :)
http://www.crockford.com/javascript/remedial.html

BFN,

fp.
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 250 total points
ID: 10936117
...specifically:

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isFunction(a) {
    return typeof a == 'function';
}


BFN,

fp.
0
 

Author Comment

by:Darren_Fryer
ID: 10936197
Thank you very much!
isObject worked brilliantly.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10936222



  rBttn = document.form1.PackageID;
  if(rBttn){
     if(rBttn.length){
         for(i=0; i<rBttn.length; i++) {
              if(rBttn[i].checked==true) {
                   if((rBttn[i].value)!='') {
                        PackageSelected = 1;
                   }
              }
         }
    }else{
       if(rBttn.checked && rBttn.value!=""){
         PackageSelected = 1;
       }
    }
  }



0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 10936284
Hi again,

Thanks for the points/grading.

Happy codin',

BFN,

fp.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 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