Solved

Javascript manipulation of checkboxes does not work in IE

Posted on 2007-03-22
8
160 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
[X]
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
  • 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

717 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