Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

multiple checkboxes, 1 ID, getElementById values...

Posted on 2006-12-01
6
Medium Priority
?
1,547 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

877 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