Solved

multiple checkboxes, 1 ID, getElementById values...

Posted on 2006-12-01
6
1,536 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
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