Wednesday, June 27, 2012

Check All Check None Javascript

A checkall/check none javascript implementation that doesn't care about ASP.

You ready?


  • Check all
  • Check me!
  • Check me, too!
  • Check this third box.
<script type="text/javascript">
function checkall(id,value) {
   var form = document.getElementById(id);
   for (var n=0; n< form.length; n++)
     if (form.elements[n].type == 'checkbox')
        form.elements[n].checked = value;
  return false;
}
</script>
<form id="myForm">
<ul>
<li><input type="checkbox" onclick="checkall('myForm', this.checked)">Check all</li>
<li><input type="checkbox">Check me!</li>
<li><input type="checkbox">Check me, too!</li>
<li><input type="checkbox">Check this third box.</li>
</ul>
</form>
Code markup via quickhighlighter.com

Tuesday, June 26, 2012

Pass a previous value in a select box

Renamed: Set a default select option when normal default is blank.
Let's say you want to do an event on a select onclick ... maybe something like a select with a blank as default, but if it's clicked, you want to change it to some other default (today's month?) ... but you also want that original blank to be selectable to "erase" the entry. This does not require jQuery or ASP. No need for a library to do this. Use it how you want.



    <script type="text/javascript">
        function setDefault(el) {
            if (el.previousValue != "") {
                return;
            }
            if (el.value) {
                return;
            }
            if (el.value == "") {
                el.value = "choose me"
            }
        }
    </script>
    <select onclick="setDefault(this)" onfocus="this.previousValue=this.value">
        <option value=""></option>
        <option value="skip me">skip me</option>
        <option value="choose me">choose me</option>
    </select>
Code marked up with quickhighlighter.com

Blog Archive