Solved

Javascript Function will not work in IE9 or Firefox

Posted on 2013-01-30
3
515 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
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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

680 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