Thursday, September 13, 2012

manhandling a table with jQuery

This is some JavaScript (and other stuff) that I began rolling in a SharePoint web part yesterday. This code shows off jQuery for finding the nth td in a tr, finding an input field inside a parent, collecting the rows of a table in a collection, getting the current time, and hiding table rows.

<div class="me-first">
   Please answer two questions to begin...
   <div class="whatever">
      <strong>Question One:</strong> Do you like cats?
      <div style="padding: 0px 0px 0px 150px;">
         <input type="radio" name="question1" value="xYes"> Yes<br />
         <input type="radio" name="question1" value="xNo"> No
      </div>
   </div>
   <div class="whatever">
      <strong>Question Two:</strong> Do you like dogs?
      <div style="padding: 0px 0px 0px 150px;">
         <input type="radio" name="question1" value="yYes"> Yes<br />
         <input type="radio" name="question1" value="yNo"> No
      </div>
   </div>
</div>
<script type="text/javascript" src="http://www.hdri.net/Scripts/jquery-1.4.4.min.js">
</script>
<script type="text/javascript">
   $(function() {
      $(".me-first").attr('style', 'display: block !important;');
      $('input[name=question1]').keypress(function (e) {
         switch (e.keyCode) {
            case 37:
               unhideform();
               break;
            case 38:
               unhideform();
               break;
            case 39:
               unhideform();
               break;
            case 40:
               unhideform();
               break;
         }
      });
      $('input[name=question1]').bind('click', function () {
         unhideform();
      });
      $('input[name=question2]').keypress(function (e) {
         switch (e.keyCode) {
            case 37:
               unhideform();
               break;
            case 38:
               unhideform();
               break;
            case 39:
               unhideform();
               break;
            case 40:
               unhideform();
               break;
         }
      });
      $('input[name=question2]').bind('click', function () {
         unhideform();
      });
   });
   function unhideform() {
      var checkx = "" + $('input[name=question1]:checked').val();
      var checky = "" + $('input[name=question2]:checked').val();
      if (checkx.indexOf("x") >= 0 && checky.indexOf("y") >= 0) {
         var now = new Date();
         var months = new Array('1','2','3','4','5','6','7','8','9','10','11','12');
         var today = months[now.getMonth()] + "/" + now.getDate()+ "/" + now.getYear();
         var formtablerows = $(".ms-formtable").attr('rows');
         
         /* set registration date */
         var detail0a = $(formtablerows[0]).find("td").eq(2);
         var detail0b = $(detail0a).find('input:text:first');
         detail0b.val(today);
         $(formtablerows[0]).hide();
         
         /* show revamped form */
         $(".me-first").attr('style', 'display: none !important;');
         $(".ms-formtable").attr('style', 'display: block !important;');
         $(".ms-toolbar").attr('style', 'display: block !important;');
      }
   }
</script>

No comments:

Post a Comment