Solved

Javascript Function will not work in IE9 or Firefox

Posted on 2013-01-30
3
479 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Yes,  it worked perfect.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now