Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1723 How can I redirect to another page depending on how many checkboxes have been checked?

You are here: irt.org | FAQ | JavaScript | Form | Q1723 [ previous next ]

Try the following which loads the 1-3.htm page if one, two or three checkboes have been checked, the 4-6.htm page if four, five or six, and the 7-9.htm page if seven, eight or nine checkboes have been checked:

<html>

<head>

<script language="JavaScript"><!--
function redirect (form, group) {
  var numberClicked = 0;
  for (var i=0; i<form.elements.length; i++) {
    if (form.elements[i].name == group && form.elements[i].type == 'checkbox' && form.elements[i].checked)
      numberClicked++;
  }
  if      (numberClicked <= 3) url = '1-3.htm'
  else if (numberClicked <= 6) url = '4-6.htm';
  else                         url = '7-9.htm';
  location.href = url;
  return false;
}
//--></script>

</head>

<body>

<form onSubmit="return redirect(this,'group1')">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="checkbox" name="group1">
<input type="submit" value="Submit">
</form>

</body>

</html>

Feedback on 'Q1723 How can I redirect to another page depending on how many checkboxes have been checked?'

©2018 Martin Webb