tags:

views:

150

answers:

4

The below javascript is working fine in IE but not in Mozilla. We have a header checkbox,onClick of which all the other checkboxes in the page gets selected/unselected. Can anyone help?

 <script language="javascript">
  function SelectAll() {
      var frm = document.forms[0];
      var j = frm.elements.length;
      var checkAll = document.getElementById("checkAll");
      var checkBoxCount = 0;

      if(checkAll.checked == true) {
          var i = 0;
          while(i != j) {
              if (frm.elements[i].type == "checkbox") {
                  frm.elements[i].checked = true;
                  checkBoxCount++;
              }
              i++;
          }

          var chkAll = document.getElementById("checkAll");
          chkAll.checked = true;
      } else {
          var i = 0;
          while(i != j) {
              if (frm.elements[i].type == "checkbox") {
                  frm.elements[i].checked = false;
              }
              i++;
          }
          var unchkAll = document.getElementById("checkAll");
          unchkAll.checked = false;
      }
  }
A: 

If you don't have a strong reason not to, I suggest using a JavaScript library such as jQuery, MooTools etc. They are built with multi-browser compatibility in mind and make doing things like what you are trying to do really trivial.

code_burgar
+3  A: 

Ok, here's a much better way to do it. In your HTML, make your checkbox like this:

<input type="checkbox" onclick="selectAll(this)" />

And then your Javascript:

function selectAll(checkbox) {
    var form = checkbox.form;
    for (var i = 0, l = form.elements.length; i < l; ++i) {
        if (form.elements[i].type == 'checkbox') {
            form.elements[i].checked = checkbox.checked;
        }
    }
}

Here it is for you to test out: http://jsbin.com/idadi

If this is going into an XSLT (for whatever reason), then the simplest way to make sure the < doesn't stuff it up is to make it CDATA, like this

<someElement><![CDATA[
    function selectAll() { 
        // as above
    }
]]></someElement>
nickf
+1; it should read `for(var i = ...)`, though!
Christoph
thanks - edited now
nickf
Thanx Nickf. This solution works for me in .net.But in an xslt file, its unable to recognize the '<' symbol. So cant implement it there. Is there any way to replace ur code without using < symbol?
archana roy
using a CDATA section will work in XSLT.
nickf
Nick...how do we use this CDATA in xslt?
archana roy
A: 

You can replace you function with something similar to this. Here's a Working Demo that works for me in Firefox. add /edit to the URL to see the code

  function SelectAll() {
      var frm = document.forms[0];
      var j = frm.getElementsByTagName('input');
      var checkAll = document.getElementById("checkAll");

      for (var i=0; i < j.length; i++) {
        if (j[i].type == "checkbox") {
          j[i].checked = checkAll.checked; 
        }
      }             
  }

EDIT:

If you need to avoid the use of the < symbol in the code, it could be rewritten like so (I've tidied up some of the other code too).

  function SelectAll() {
      var elements = document.forms[0].elements;
      var i = elements.length;
      var checkAll = document.getElementById("checkAll");

      while (i--) {
        if (elements[i].type === "checkbox")
          elements[i].checked = checkAll.checked;
      }

  }
Russ Cam
the check for `.type == "checkbox"` should be within the loop: you don't want to terminate on non-checkbox elements!
Christoph
Thanx Russ.The code works fine in .net.But in an xslt file,its unable to recognize the '<' symbol.So is there any way we can use ur code but without the '<' symbol?
archana roy
@Christoph - Thanks, I wasn't thinking!
Russ Cam
Thanx a ton Russ
archana roy
no probs. The while loop works as 0 is a falsy value. Therefore when decrementing i and it's value reaches 0, the loop stops.
Russ Cam
Russ,actually in Mozilla we are not able to recognize the below object...and hence no code is running.Do you have a fix ? var checkAll = document.getElementById("checkAll");I tried to alert(checkAll) after the above line. In IE it returns- object but in MOzilla it returns-null.
archana roy
Did you say it's in an XSLT file? have you tried with single-quotes instead of double-quotes? e.g. checkAll = document.getElementById('checkAll'); and also if (elements[i].type === 'checkbox').
Russ Cam
Yes i have tried. The problem is with the --var checkAll = document.getElementById("checkAll"); it doesnt work in mozilla
archana roy
using a CDATA section is a *much* better solution than refactoring your code to avoid certain characters!!
nickf
@archana - it does work in Mozilla as demonstrated in the Working example and verified by the documentation - https://developer.mozilla.org/en/DOM/document.getElementById .The problem is elsewhere. Mozilla is stricter than IE for JavaScript syntax and will fail at the point a syntactical error is found. Run your code through JSLint to find any obvious errors http://www.jslint.com/
Russ Cam
@nickf- very good point!
Russ Cam
Thanks Russ and Nick for ur help.
archana roy
A: 

I simplified your code into just this:

function SelectAll() {
   var elements = document.forms[0].elements;
   var check = document.getElementById("checkAll").checked;
   for (var i = 0; i < elements.length; i++) {
      if (elements[i].type == "checkbox") {
         elements[i].checked = check;
      }
   }
}

I tried it in Firefox, and it works.

Guffa
Thanks Guffa for the help.
archana roy