Tuesday, April 17, 2012

[Javascript] Checkbox Check/Uncheck All Toggle Switch

Checkbox are useful when you intend to use more than one response or data so what if the user want to check all the available info? This is where the toggle switch happened to be useful.

Here's the code:
<script language="javascript">
  function checkAll(list, tick)
  {
    for (i = 0; i < list.length; i++)
      list[i].checked = tick.checked? true:false
  }
</script>
<form id="mylist" name="list">
  <input type="checkbox" name="toggle" onclick="
    javascript:checkAll(document.list.check, this)" />All
  <input type="checkbox" name="check" value="one" />One
  <input type="checkbox" name="check" value="two" />Two
  <input type="checkbox" name="check" value="three" />Three
  <input type="checkbox" name="check" value="four" />Four
  <input type="checkbox" name="check" value="five" />Five
</form>

Result:
All One Two Three Four Five

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...