Tuesday, August 24, 2010

JQuery & Checkboxes


 Basically
  • Creating Checkbox
  • Applying custom attribute to checkbox
  • Applying unique ID to checkbox
  • Checking of the checkbox is checked
  • Applying style to checkbox
  • checking a  checkbox
all via JQuery



   1:  <script language="javascript" type="text/javascript">
   2:   
   3:      $(document).ready(function () {
   4:          var cbID = 0;
   5:          var totalPeriod = 11;
   6:   
   7:          //on rollver create checkbox
   8:          $("#activity0").bind("mouseenter mouseleave", function (e) {
   9:              cbID++;
  10:              $(this).append("<input type='checkbox' id='myCB_" + cbID + "' cbtype='firstOne'/>");
  11:          });
  12:   
  13:          for (var x = 1; x < totalPeriod; x++) {
  14:              $("#activity").append("Period#" + x + " <input type='checkbox' id='myCB_" + x + "' cbtype='firstOne'/>");
  15:          }
  16:   
  17:          //when btn is clicked
  18:          $("#processBtn").bind("click", function (e) {
  19:              alert("Showing All CHeckBox")
  20:              $("form input:checkbox").each(function (i) {
  21:                  alert($(this).attr('id') + " " + $(this).attr('cbType') + " - " + $(this).attr('checked'))
  22:              });
  23:          });
  24:   
  25:          //toggle style on CB
  26:          $("form input:checkbox").bind("focus", { cssStyle: "focusCB" }, setCSS);
  27:          //$("form input:checkbox").bind("blur", { cssStyle: "blurCB" }, setCSS);
  28:   
  29:   
  30:          function setCSS(e) {
  31:              var cbID = "#" + e.target.id;
  32:              $(cbID).toggleClass(e.data.cssStyle);
  33:              $(cbID).attr("disabled", "disabled");
  34:              $(cbID).attr("checked", "checked");
  35:              $("#checkStatus").append("<br>You Checked: " + $(cbID).attr("id"));
  36:          }
  37:      });
  38:   
  39:  </script>
  40:   
  41:  <div id="activity">
  42:      Activity Area
  43:      <br />
  44:    
  45:      </div>
  46:      <input type="checkbox" disabled="disabled" />
  47:      <input type="button" id="processBtn" value="go" />
  48:   
  49:      <div id="checkStatus">
  50:      Status
  51:     
  52:      </div>

No comments: