PDA

View Full Version : Everything works great in IE but not in FF



livegenesis
01-28-2011, 02:16 AM
I have three java functions running on a form and everything works great, but I cannot figure out why Firefox isn't working... This is very frustrating... Any suggestions would be greatly appreciated. Thanks!

Link to form (http://n2nsites.com/index.php?option=com_rsform&view=rsform&Itemid=9)

livegenesis
01-28-2011, 04:47 AM
Okay, I have an update. It seems that the problem may be the getelementsbyname that I am using. After looking through several other forums, I ran across a post that was kind of related to that being the cause. Anyways, it got me thinking an I have come to the conclusion that it must be that or something around those in my code. I just don't know how else to get the values from the radios I am using... Any advise?

Thanks!

gindian
01-28-2011, 05:23 AM
document.getElementById('').innerHTML=document.getElementById("ListCost0");
document.getElementById('').innerHTML=document.getElementById("ListCost1");

Philip M
01-28-2011, 07:25 AM
I have three java functions running on a form

Just a reminder - Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. :)

It would help if you indicated what the problem in Firefox actually is.

You should assign a name to your form and its elements - there is no point in assigning an id to a form element. And then access the form elements in the standard way - document.formname.elementname.value.
To capture the value of a radio button within a group, you need to loop through the elements to find the one which is checked.

livegenesis
01-28-2011, 09:12 AM
I appreciate the help guys, but I got it figured out. Okay, so first thing is Firefox, Safari, Chrome, etc... Anything not IE, doesn't like getElementsByName(""). It apparently is unreliable and seems to be outdated by other methods. IE is just as unreliable and outdated, too. ;)

So, here is what I did for anyone who may experience this issue.

Original:

var j;
for(j=0;j<document.getElementsByName('form[ListOption1][]').length;j++)
if (document.getElementById('ListOption1'+j).checked)
document.getElementById('listoptionP').innerHTML += " " + document.getElementById('ListOption1'+j).value;
}

New:

if (document.getElementById('ListOption10').checked) {
document.getElementById('listoption1P').innerHTML = document.getElementById('ListOption10').value;
}
if (document.getElementById('ListOption11').checked) {
document.getElementById('listoption2P').innerHTML = document.getElementById('ListOption11').value;
}
}

Also...

var i;
var list = 0;
var listcost = document.getElementsByName('form[listcost]');
var totalBudget = document.getElementById('TotalBudget').value;
var postage = document.getElementById('Postage').value;
var printing = document.getElementById('Printing').value;
var uses = document.getElementById('Uses').value;

var totalRecords = document.getElementById('TotalRecords');

for (var i = 0; i < listcost.length; i++) {
if (listcost[i].checked) {
if (listcost[i].value == "Single") {
list = 0.10;
}
else {
list = 0.13;
}
}
}

Changed to:

var list = 0;
var costopt1 = document.getElementById('ListCost0');
var totalBudget = document.getElementById('TotalBudget').value;
var postage = document.getElementById('Postage').value;
var printing = document.getElementById('Printing').value;
var uses = document.getElementById('Uses').value;

var totalRecords = document.getElementById('TotalRecords');


if (costopt1.checked) {
list = 0.10;
}
else {
list = 0.13;
}

}

And...

var cost = document.getElementsByName('form[listcost]');
var uses = document.getElementById('Uses');

if (uses.value == 1) {
cost[0].checked = true;
}
else {
cost[1].checked = true;
}
}

Changed to:

var cost1 = document.getElementById('ListCost0');
var cost2 = document.getElementById('ListCost1');
var uses = document.getElementById('Uses');

if (uses.value == 1) {
cost1.checked = true;
}
else {
cost2.checked = true;
}
}

Thanks again for the suggestions and I hope this helps others avoid using getElementsByName("").

Kor
01-28-2011, 11:37 AM
document.getElementsByName() is as reliable as any other DOM methods, and it is crossbrowser. Except that you should use it only for the elements which are entitle to bear a name attribute. A DIV or a SPAN, for instance, can not bear a name. As a general rule it is better to use name attributes only in case of the elements of a form (but not for the form itself).



You should assign a name to your form and its elements - there is no point in assigning an id to a form element.

That is not valid in all the Doctypes. Under a XHTML Doctype, the FORM can not bear a name. Only an id. That could have provoked the errors in the livegenesis's code. I guess he used document.getElementsByName() to refer a FORM upon its name, which is not allowed under the DTD he use (which is XHTML)