1

I don't know why I'm unable to toggle my checkbox with the code mentioned below. But the same code was working fine in another project. But, when I try to implement it in another CodeIgniter project, it doesn't work. Please help me to solve this issue.

 <form action=""  method="post" onSubmit="return checkTheBox() " name="checkboxform">';

Onclick event in a href

    echo "<td><a href='javascript:void();' onClick='javascript:checkAll('checkboxform', true);'><b>Check All</b></a> | 
     <a href='javascript:void();' onClick='javascript:checkAll('checkboxform', false);'><b>UnCheck All</b></a></td>";

Checkbox

   echo "<td><input type='checkbox' name='checkid[]' value=''</td>";

Javascript

  <script type="text/javascript">

   function checkAll(formname, checktoggle)
   {
   var checkboxes = new Array();
   checkboxes = document[formname].getElementsByTagName('input');
  for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].type === 'checkbox') {
           checkboxes[i].checked = checktoggle;
          }
      }
  }
  </script>
Sampson
  • 265,109
  • 74
  • 539
  • 565
shankar
  • 11
  • 3

1 Answers1

1

I can see some problems with your code

  • input tag is not closed, missing >, should be

    <td><input type='checkbox' name='checkid[]' value=''></td>
    
  • The onclick attribute and the javascript string in the a elements are all enclosed in single quotes '

    onClick='javascript:checkAll('checkboxform', true);'
    

    For the browser the onclick attribute value is 'javascript:checkAll(' and checkboxform', true);' is just garbage, it doesn't understand.

    This should be something like

    onClick="javascript:checkAll('checkboxform', true);"
    

    and in PHP this would be

    echo "... onClick=\"javascript:checkAll('checkboxform', true);\" ..."
    
  • href='javascript:void(); should be href="javascript:void(0);

When you fix these errors, it works as expected. See JSFiddle

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Btw you (I mean the OP) could cleanup all these obsolete `javascript:` and `language=text/javascript` things. It makes the code less readable for no good reason. – kuroi neko Jan 09 '14 at 14:54
  • at Olaf (actually i am echoing the html elements sir like this) echo "Check All | UnCheck All"; – shankar Jan 09 '14 at 14:59
  • @shankar Yes, but it doesn't matter, how you create the attributes. The important part is, you use the same quotes `'` for enclosing the attribute value and the Javascript string. This confuses the browser, since it doesn't know where the attribute value *really* ends. See also updated answer. – Olaf Dietsche Jan 09 '14 at 15:06
  • @Olaf,,,,,,fine,,,,so i will change the javascript attribute from ' to ",,like onClick='javascript:checkAll("checkboxform", false);',,,when i did,it says error – shankar Jan 09 '14 at 15:08
  • but now,,when i changed all my php to normal html tags and single quotes to double quotes for java script value;;its working fine,,,, – shankar Jan 09 '14 at 15:11
  • this is a small issue,but i cant fix it up,when i am echoing with php single tag the whole html elements – shankar Jan 09 '14 at 15:12
  • @shankar Maybe this question helps understand the issue http://stackoverflow.com/q/7999148/1741542 – Olaf Dietsche Jan 09 '14 at 15:13
  • anyow,,thx a lot OLAF for ur post and made me think.....nd do u know any website to learn codeigniter for all form elements and how to call javascript libraries in codeigniter – shankar Jan 09 '14 at 15:13
  • @shankar I learned using Codeigniter from the documentation at http://ellislab.com/codeigniter and http://ellislab.com/codeigniter/user-guide/ – Olaf Dietsche Jan 09 '14 at 15:16
  • alright,,,,i have swapped my double quotes and single quotes,,,,now i understood,,,,thx Olaf – shankar Jan 09 '14 at 15:16