Solved

multiple checkboxes, 1 ID, getElementById values...

Posted on 2006-12-01
6
1,532 Views
Last Modified: 2008-01-09
hey all, i have a couple of checkboxes that all share the same ID. How can i run some sort of function to give me the values of the checked checkboxes. If there are none checked, then the value is 0? so...

function getChecked(idOfElement) {

   loop through elements and save checked values....

    looop.....    
    checkedValues = 1,3

  else 'no checked values
   checkedValues = 0

}


 <input name="dID14" type="checkbox" id="dID14" value="1" checked> Human Resources  
 <input name="dID14" type="checkbox" id="dID14" value="2"> Internet Technology
 <input name="dID14" type="checkbox" id="dID14" value="3" checked> Finance

document.write(return getChecked('dID14'))

One more thing, this is not in a form tag and will not be in a form tag. all the checkboxes must have the same id.

Hope this makes sense.. thanx all!
0
Comment
Question by:engineroom
  • 2
  • 2
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 500 total points
ID: 18054395
<input name="dID14" type="checkbox" id="dID14" value="1" checked> Human Resources  
 <input name="dID14" type="checkbox" id="dID14" value="2"> Internet Technology
 <input name="dID14" type="checkbox" id="dID14" value="3" checked> Finance
<script language=javascript>
function getChecked(idOfElement) {
      var arr = new Array();
      var allCheckBoxes = document.getElementsByTagName('input');
      for( i = 0; i < allCheckBoxes.length; i++) {
            var checkbox = allCheckBoxes[i];
            if(checkbox.type == 'checkbox' && checkbox.id == idOfElement && checkbox.checked){
                  arr[arr.length] = checkbox.value;
            }
      }
      if(arr.length > 0) return arr;
      return 0;
}
alert(getChecked('dID14'));
</script>
0
 
LVL 3

Author Comment

by:engineroom
ID: 18054532
Thanx, i actually figured it out, here's what i did, tell me what you think.

function getChecked(dID){

      var checkGroup=document.getElementsByName(dID);
      checkList = "0"

      for(var i = 0; i < checkGroup.length; i++){
      checkItem = checkGroup.item(i);
            if (checkItem.checked==true){
                  checkList += "," + checkItem.value;
            }
      }

      alert(checkList);

}
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 18054581
this will return 0,1,3 in your case i think
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:enachemc
ID: 18054617
document.getElementsByName is not compatible with all browsers:
http://www.quirksmode.org/dom/w3c_core.html
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 18054640
this will give it in the way you asked in your question


        var result="";
        var x=document.getElementsByName("dID14")
        alert(x.length + " elements!")

        for(var i=0;i<x.length;i++)
        {
              if(x[i].checked==true)
                result = result + x[i].value + ",";
        }

        if(result=="")
        return "0";
        else
        return result.substring(0,result.length-1);

0
 
LVL 3

Author Comment

by:engineroom
ID: 18055225
Thanx guys.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now