PDA

View Full Version : question bout ie and firefox behaviour on javascript scripts



awakener1986
03-17-2010, 08:43 AM
well firstly, im having this problem where by the javascipt script runs perfectly fine on Firefox browser but it does'nt on IE 8 browsers.

here's the script :


var i,zform = document.listcases

for (i=1; i<zform.elements.length;i++) {
if (zform.elements[i].id == 'cases[]'){
if(zform.elements[i].checked == true){
txtid = zform.elements[i].value.split(':')
url += txtid[1] + ','
x++
}
}
}

cases[] is an array of checkboxes which i would like to detect whether they are checked or not. in IE the loop seems to not able to detect the cases[] elements.

need help on this problem. thanks

jscheuer1
03-17-2010, 10:41 AM
Both 'url' and 'x' are undefined. As a result, the code won't work in any browser.

Now, they may be defined elsewhere that you are not showing us. If this code works in Firefox, they probably are.

Just guessing though, rather than test for id == 'cases[]', if the elements are named cases[], as would be proper HTML syntax, test for name == 'cases[]'.

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.

jscheuer1
03-17-2010, 10:55 AM
I did a little experiment. In both IE and Firefox, this 'works':


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
onload = function(){
var c = document.listcases.elements;
for (var i = c.length - 1; i > -1; --i){
if(c[i].id == 'cases[]'){
alert('works');
}
}
}
</script>
</head>
<body>
<form action="#" name="listcases">
<input type="checkbox" id="cases[]" value="">
<input type="checkbox" id="cases[]" value="">
</form>
</body>
</html>

So it's not IE's inability to pick up on the invalid id="cases[]" elements, there must be something else that you are not showing us.

awakener1986
03-17-2010, 11:18 AM
yes both the url and x are defined somewhere else.

but those 2 variables are'nt really that vital. what i really need is to be able to detect the 'cases[]' element with the if statement. these are 2 checkboxes that i have in my html

<input type="checkbox" id="cases[]" name="cases[]" value="testing1" checked>

<input type="checkbox" id="cases[]" name="cases[]" value="testing2" checked>

and yea i've already changed id == 'cases[]' to name == 'cases[]' and the problem is still there.

jscheuer1
03-17-2010, 02:00 PM
Still 'works' here in IE:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
onload = function(){
var c = document.listcases.elements;
for (var i = c.length - 1; i > -1; --i){
if(c[i].id == 'cases[]' && c[i].checked == true){
alert('works');
}
}
}
</script>
</head>
<body>
<form action="#" name="listcases">
<input type="checkbox" id="cases[]" name="cases[]" value="testing1" checked>

<input type="checkbox" id="cases[]" name="cases[]" value="testing2" checked>
</form>
</body>
</html>

Sounds like there might be a problem (in your version) with the undeclared variable 'txtid'. Try changing:


txtid = zform.elements[i].value.split(':')

to:


var txtid = zform.elements[i].value.split(':')

And testing1 (the value of the checkbox) when split on ':' will have an index [1] of undefined. So (again, in your version):


url += txtid[1] + ','

would result in 'undefined,' or some other falsey string being added to the existing value of 'url'. Or it might even cause an error.

But without seeing the page, I can only guess:

Please post a link to a page on your site that contains the problematic code so we can check it out.

awakener1986
03-18-2010, 03:52 AM
thanks for ur help ... im sorry i could'nt show more of my codes. but i kind of found the problem it seems there's another form within the listcases form.

<form action="#" name="listcases">
<form action "#" name="otherform">
</form>
<input type="checkbox" id="cases[]" value="">
<input type="checkbox" id="cases[]" value="">
</form>


i think this is causing a problem in IE but not in Firefox. i would like to ask is there some way to detect all the elements without specifying the form ?? is it possible to get all elements on the page?

something like that?

document.elements;


thanks again.

jscheuer1
03-18-2010, 12:25 PM
<form action "#" name="otherform">
</form>
<form action="#" name="listcases">
<input type="checkbox" id="cases[]" value="">
<input type="checkbox" id="cases[]" value="">
</form>