javascript - How to save Checkbox Value and status -


i need save values status of checkboxes on page; can check @ time of submit if checked box has been unchecked , use value using comparestatus() function further logic. have tried , error variable periodchk not defined.

<input type="checkbox" name="periods" value="25301601" > <input type="checkbox" name="periods" value="25301602"  checked>  <script type="text/javascript">     $j(document).ready( function() {         var periodchk;         $j(":checkbox").each( function() {                                       periodchk["$j(this.id).val()"]=($j(this).is(':checked'));         } );          console.log(periodchk);          function comparestatus() {             var periodstring;              if (!$j(this).is(':checked')) {                 // if 1 checked before, grab , make string                 if (window.periodchk["$j(this.id).val()"])                  {                     periodstring+=["$j(this.id).val()"] + ",";                 }             }         }     } ); </script> 

your periodchk variable (correctly) declared using var inside document.ready module, exists inside function scope. can access through:

if (periodchk["$j(this.id).val()"])

(note: in order have variable accessible in global scope (window) either have declare without var or explicitly assign window. however, bad practice.)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -