checkbox values in javascript?

experts,

How can I get checkbox values in Javascript?

function passit()
{

myhazard = document.all.hazard.checked;<--------??????
alert(myhazard);
theResult = myhazard

window.returnValue = theResult
close();
}
suran78Asked:
Who is Participating?
 
thirdConnect With a Mentor Commented:
here's an example,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Author: Third Santor</title>
<script>
  function getCheckboxValues(obj){
    for(var i=0; i<obj.length; i++){
        if((obj.elements[i].type == 'checkbox') && (obj.elements[i].checked)){
          alert(obj.elements[i].value);
        }
      }
  }
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<form name="form1" method="post" action="" onsubmit="">
<input type="checkbox" name="chk1" value="value1">
<input type="checkbox" name="chk1" value="value2">
<input type="checkbox" name="chk1" value="value3">
<input type="button" value="Click" onclick="getCheckboxValues(this.form);">
</form>
</body>
</html>
0
 
thirdCommented:

myhazard = document.formname.hazard.value;

if you want to get the value for checked checkbox,

myhazard = document.formname.hazard.checked?document.formname.hazard.value:'';
0
 
suran78Author Commented:
Thanks this is working.

i have another open question at http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21877097.html
0
 
suran78Author Commented:
another small question:

How can I index a row in Javascript?

hazard[rowid].innertext = values  <-------------??????


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.