Solved

Javascript Function will not work in IE9 or Firefox

Posted on 2013-01-30
3
518 Views
Last Modified: 2013-02-04
Hello,

I just recently notcied that this javascript does not work in IE 9 ore firefox
<script language="javascript" type="text/javascript">
window.onload = function() {

    document.getElementById("join_gold_form_c").onsubmit = function() {
		var howManyChecked = nbCheckboxChecked();
        if(howManyChecked!=3) {
            alert("select 3 checkboxes, no more no less!");
        	return false;
        }
        return true;
    }

    var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++)  if( inputs[i].name && /^chkCompany\d$/.test(inputs[i].name) ) inputs[i].onclick = function() {
        if(nbCheckboxChecked()==3) disableCheckboxes(true);
        else disableCheckboxes(false);
    };
}

function nbCheckboxChecked() {
    n = 0;
	var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++) if( inputs[i].name && /^chkCompany\d$/.test(inputs[i].name) && inputs[i].checked ) n++; 
    return n;
}

function disableCheckboxes(b) {
	var inputs = document.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++) if( inputs[i].name && /^chkCompany\d$/.test(inputs[i].name) ) inputs[i].disabled = !inputs[i].checked && b;
}
</script>

Open in new window


Here is the html for it:
<table>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" formenctype=="application/x-www-form-urlencoded" name="join_gold_form_c" target="_self"  onsubmit="function();"/>
<tr><td><input name="chkCompanyA" type="checkbox" id="chkCompanyA" value="ATH">Athene</td></tr>
<tr><td><input name="chkCompanyB" type="checkbox" id="chkCompanyB" value="ET">EquiTrust</td></tr>
<tr><td><input name="chkCompanyC" type="checkbox" id="chkCompanyC" value="FLIC">Forethought</td></tr>
<tr><td><input name="chkCompanyD" type="checkbox" id="chkCompanyD" value="PHX">Phoenix</td></tr>
<tr><td><input name="chkCompanyE" type="checkbox" id="chkCompanyE" value="NWL">National Western</td></tr>
<tr>
<td valign="top" colspan="2" align="center" bgcolor="#FFFFFF"><input name="btnSubmit" type="submit" id="btnSubmit" value="Submit" onClick="$openContractLink" ></td>
			</tr>
</table></form> 

Open in new window



Help!
0
Comment
Question by:JParra72
[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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38837572
replace your javascript with the following :
<script type="text/javascript">
window.onload = function() {

    document.getElementsByName("join_gold_form_c")[0].onsubmit = function() {
		var howManyChecked = nbCheckboxChecked();
        if(howManyChecked!=3) {
            alert("select 3 checkboxes, no more no less!");
        	return false;
        }
        return true;
    }

    var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++)  if( inputs[i].name && /^chkCompany.$/.test(inputs[i].name) ) inputs[i].onclick = function() {
        if(nbCheckboxChecked()==3) disableCheckboxes(true);
        else disableCheckboxes(false);
    };
}

function nbCheckboxChecked() {
    n = 0;
	var inputs = document.getElementsByTagName("input");
    for(var i=0;i<inputs.length;i++) if( inputs[i].name && /^chkCompany.$/.test(inputs[i].name) && inputs[i].checked ) n++; 
    return n;
}

function disableCheckboxes(b) {
	var inputs = document.getElementsByTagName("input");
	for(var i=0;i<inputs.length;i++) if( inputs[i].name && /^chkCompany.$/.test(inputs[i].name) ) inputs[i].disabled = !inputs[i].checked && b;
}
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 38837645
work fine fin on IE10 and Firefox : http://fiddle.jshell.net/3Dvaf/3/show/
source code : http://jsfiddle.net/3Dvaf/3/
0
 

Author Closing Comment

by:JParra72
ID: 38851442
Yes,  it worked perfect.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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