Solved

multiple checkboxes, 1 ID, getElementById values...

Posted on 2006-12-01
6
1,544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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