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;
}
}
-
2Install firebug and debug it on mozilla...Joachim Sauer– Joachim Sauer2009年09月30日 09:14:28 +00:00Commented Sep 30, 2009 at 9:14
-
This works fine for me in FF 3.5.rahul– rahul2009年09月30日 09:37:26 +00:00Commented Sep 30, 2009 at 9:37
-
the check/uncheck functionality doesnt work in Mozillaarchana roy– archana roy2009年09月30日 09:45:14 +00:00Commented Sep 30, 2009 at 9:45
-
1every 15 minutes, someone posts an answer with basically the same code; should I wait another 15 minutes for 'my' version?Christoph– Christoph2009年09月30日 09:56:52 +00:00Commented Sep 30, 2009 at 9:56
-
Christoph..do u have any other version?archana roy– archana roy2009年09月30日 10:07:25 +00:00Commented Sep 30, 2009 at 10:07
4 Answers 4
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>
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;
}
}
10 Comments
.type == "checkbox" should be within the loop: you don't want to terminate on non-checkbox elements!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.
Comments
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.