Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Javascript manipulation of checkboxes does not work in IE

Posted on 2007-03-22
8
157 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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