Solved

Javascript manipulation of checkboxes does not work in IE

Posted on 2007-03-22
8
156 Views
Last Modified: 2010-04-16
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
Comment
Question by:andrewaiello
  • 5
  • 3
8 Comments
 

Expert Comment

by:blackbear-arg
ID: 18771993
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
 
LVL 1

Author Comment

by:andrewaiello
ID: 18772234
I tried that exactly and it did not take affect.
0
 

Expert Comment

by:blackbear-arg
ID: 18772332
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:andrewaiello
ID: 18772402
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
 

Accepted Solution

by:
blackbear-arg earned 125 total points
ID: 18772466
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
 

Expert Comment

by:blackbear-arg
ID: 18772477
I forgot about it; you should also remove the window.onload =bbauc; line as it is pointless and will just pollute the code.
0
 
LVL 1

Author Comment

by:andrewaiello
ID: 18772808
Thanks ;)   You are the man.
0
 

Expert Comment

by:blackbear-arg
ID: 18773017
I'm at your service.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question