• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Javascript manipulation of checkboxes does not work in IE

Hello all,

     In an html page I have javascript code that unchecks all checkboxes in a form (useful for when the page reloads in my case).  This code does not work in IE.  The code is as follows:  
(all the checkboxes have the name chbx):

-----------------------------------------------

<script type="text/JavaScript">
for (i=0;i<12;i++)
   {
         document.forms[0].elements.chbx[i].checked=false;
   }
</script>

------------------------------------------------

Is there a better cross browser way of doing this?  If not I know how to detect browsers in which case i only need how to do this specific to IE.  Thank you.


0
andrewaiello
Asked:
andrewaiello
  • 5
  • 3
1 Solution
 
blackbear-argCommented:
You should try using getElementsByTagName("input"); then iterating through it unchecking all elements which have "chbx" as their name.

Something like this:

<script type="text/javascript">
//the function
function bbauc() {
 var c = document.getElementsByTagName("input");
 var f;
 for (f=0;f<c.length;f++){
  if(c[f].type == "checkbox" && c[f].name == "chbx"){
   c[f].checked = false;
  }
 }
}
//how to call it. In this case I attached the function to the .onload event of the browser
 window.onload = bbauc;
</script>
0
 
andrewaielloAuthor Commented:
I tried that exactly and it did not take affect.
0
 
blackbear-argCommented:
That's strange; I've just tried it around a minute ago in IE6, firefox and Opera.


Hmm... did you put the script on the <head> section of the page? If so, is any other script attaching itself to the onload event?

If none of the above works, I'd need some html samples please.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
andrewaielloAuthor Commented:
the javascript was in between the head tags; and there is something else on the onload;  therefore i tried just calling the function normally, which didnt work:   The relevant source is:

------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lehr - McKeown</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
td img {display: block;}.style11 {font-size: 11px}
.style12 {font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif;}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style6 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
.style7 {font-size: 12px}
</style>

<script type="text/JavaScript">



if(!document.getElementById && document.all)
document.getElementById = function(id){ return document.all[id]}


function showHide (id)
{
var style = document.getElementById(id).style
if (style.display == "none")

       if(navigator.appName == "Microsoft Internet Explorer")
      {
            style.display = "inline";
      } else {
            style.display = "table-row";
      }
else
style.display = "none";
}
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->


function bbauc() {
 var c = document.getElementsByTagName("input");
 var f;
 for (f=0;f<c.length;f++){
  if(c[f].type == "checkbox" && c[f].name == "chbx"){
   c[f].checked = false;
  }
 }
}

window.onload = bbauc;
</script>
</head>
<body bgcolor="#B7C3CF" topmargin=0 onload="MM_preloadImages('images/index-over_r4_c1.jpg','images/index-over_r5_c1.jpg','images/index-over_r6_c1.jpg','images/index-over_r7_c1.jpg','images/index-over_r8_c1.jpg','images/index-over_r9_c1.jpg','images/index-over_r10_c1.jpg','images/index-over_r11_c1.jpg','images/index-over_r12_c1.jpg','images/index-over_r13_c1.jpg','images/index-over_r14_c1.jpg')">
0
 
blackbear-argCommented:
The browser will pick the last thing that was assigned to onload, so when dreamweaver hardcodes it on <body onload="whatever();"> it ignores anything you could have assigned to it previously.

Replace this block:

<body bgcolor="#B7C3CF" topmargin=0 onload="MM_preloadImages('images/index-over_r4_c1.jpg','images/index-over_r5_c1.jpg','images/index-over_r6_c1.jpg','images/index-over_r7_c1.jpg','images/index-over_r8_c1.jpg','images/index-over_r9_c1.jpg','images/index-over_r10_c1.jpg','images/index-over_r11_c1.jpg','images/index-over_r12_c1.jpg','images/index-over_r13_c1.jpg','images/index-over_r14_c1.jpg')">

With this block:
<body bgcolor="#B7C3CF" topmargin=0 onload="MM_preloadImages('images/index-over_r4_c1.jpg','images/index-over_r5_c1.jpg','images/index-over_r6_c1.jpg','images/index-over_r7_c1.jpg','images/index-over_r8_c1.jpg','images/index-over_r9_c1.jpg','images/index-over_r10_c1.jpg','images/index-over_r11_c1.jpg','images/index-over_r12_c1.jpg','images/index-over_r13_c1.jpg','images/index-over_r14_c1.jpg'); bbauc();">


I've appended the function call to the END of the body onload, so it should do ANYTHING dreamweaver wants and ALSO execute the unchecking function
0
 
blackbear-argCommented:
I forgot about it; you should also remove the window.onload =bbauc; line as it is pointless and will just pollute the code.
0
 
andrewaielloAuthor Commented:
Thanks ;)   You are the man.
0
 
blackbear-argCommented:
I'm at your service.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now