Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Function will not work in IE9 or Firefox

Posted on 2013-01-30
3
Medium Priority
?
529 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

688 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