Link to home
Start Free TrialLog in
Avatar of engineroom
engineroom

asked on

multiple checkboxes, 1 ID, getElementById values...

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!
ASKER CERTIFIED SOLUTION
Avatar of enachemc
enachemc
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of engineroom
engineroom

ASKER

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);

}
Avatar of contactkarthi
this will return 0,1,3 in your case i think
document.getElementsByName is not compatible with all browsers:
http://www.quirksmode.org/dom/w3c_core.html
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);

Thanx guys.