Solved

Javascript Function will not work in IE9 or Firefox

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 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

19 Experts available now in Live!

Get 1:1 Help Now