Solved

Javascript manipulation of checkboxes does not work in IE

Posted on 2007-03-22
8
154 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
Comment Utility
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
Comment Utility
I tried that exactly and it did not take affect.
0
 

Expert Comment

by:blackbear-arg
Comment Utility
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
 
LVL 1

Author Comment

by:andrewaiello
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Accepted Solution

by:
blackbear-arg earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks ;)   You are the man.
0
 

Expert Comment

by:blackbear-arg
Comment Utility
I'm at your service.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now