• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1834
  • Last Modified:

Javascript "is not a function" error

Hi,

I have two radio buttons, that I want to toggle whether a textbox is disabled or not. I have then running an onChange event to go to the cashOrCheck() function (code is attached). However, firebug keeps saying that "cashOrCheck is not a function", and I just can't figure out what the problem is. I have a feeling that it's a very simple answer...but it just needs a fresh set of eyes. The javascript function is in an external file, and the other functions from that file work fine and are all closed and everything.

Any help will be appreciated. Thanks!
<form action='payments.php' method='post'>
<input type='hidden' name='submitPayment' value='$paymentID'>
Payment Method:<input type='radio' name='cashOrCheck' value='cash' id='cashCheck' onChange=\"cashOrCheck()\" />Cash&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='radio' name='cashOrCheck' value='check' id='cashCheck' onChange=\"cashOrCheck()\" />Check<br />
Check Number:<input type='text' name='checkNum' id='checkNum'>
<br /><input type='submit' value='Submit'>
</form>";
 
 
function cashOrCheck()
{
var button = document.getElementById('cashCheck');
if (button.value == 'cash')
document.getElementById('checkNum').disabled = true;
else
document.getElementById('checkNum').disabled = false;
}

Open in new window

0
bkaneweb
Asked:
bkaneweb
  • 3
  • 2
2 Solutions
 
Davejk88Commented:
Try it this way:

var cashOrCheck = function() {
    var button = document.getElementById('cashCheck');
    if (button.value == 'cash') {
        document.getElementById('checkNum').disabled = true;
    } else {
        document.getElementById('checkNum').disabled = false;
    }
}

Open in new window

0
 
bkanewebAuthor Commented:
Thanks for the reply, but that solution still gives the same error. I've attached the complete javascript functinos page, maybe there was an error in one of the others that I'm missing that could be leading to this error...

I think all of the braces are closed, and the script is being imported OK...

thanks again...

function ajaxFunction(use, meeting){
	var ajaxRequest;  // The variable that makes Ajax possible!
	var meetingID = meeting;
	var page = use;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("myDECA is not supported on this browser. Sorry!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('sidebarContent');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			var el = $('sidebar');
			var myEffect = new Fx.Tween(el);
			// We change the background-color of the element
			myEffect.start('background-color', '#ecddbe', '#fff8f0');
		}
	}
	ajaxRequest.open("GET", "sidebar.php?sidebar=" + meetingID + "&page=" + page, true);
	ajaxRequest.send(null); 
}
function setRowSelected(id)
{
 	var element = document.getElementById(id);
	element.style.background='#f1e6d8'
}
function setRowUnselected(id)
{
 	var element = document.getElementById(id);
	element.style.background=''
}
function ActivateAMC()
{
 	var box1 = document.getElementById('ptvalue');
 	var box2 = document.getElementById('category');
		box1.disabled = false;
		box1.style.color='';
		box2.disabled = false;
		box2.style.color='';
}
function DeactivateAMC()
{
 	var box1 = document.getElementById('ptvalue');
 	var box2 = document.getElementById('category');
		box1.disabled = true;
		box1.style.color='#dfdfdf';
		box2.disabled = true;
		box2.style.color='#dfdfdf';
}
function checkAll(id){
   	var f = document.getElementById(id);
   	var inputs = f.getElementsByTagName("input");
   	for(var t = 0;t < inputs.length;t++){
     	if(inputs[t].type == "checkbox")
       		inputs[t].checked = true;
   	}
}
function uncheckAll(id){
   	var f = document.getElementById(id);
   	var inputs = f.getElementsByTagName("input");
  	 for(var t = 0;t < inputs.length;t++){
     	if(inputs[t].type == "checkbox")
       		inputs[t].checked = false;
   	}
}
function invertAll(){
	var f = document.getElementById('members');
	var inputs = f.getElementsByTagName('input');
	for (var t = 0; t < inputs.length; t++) {
		if (inputs[t].type == 'checkbox') {
			inputs[t].checked = !inputs[t].checked;
			alert(inputs[t].value);
		}
	}
	invertAllRows();
}
function invertAllRows(){
	var f = document.getElementById('memberList');
	var rows = f.getElementsByTagName('tr');
	for (var t = 0; t < rows.length; t++) {
		if (rows[t].style.background=='#f1e6d8')
			rows[t].style.background='';
		else
			rows[t].style.background='#f1e6d8';
	}
}
 
//REGISTRATION FORM
 
function checkUsernameForLength(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 5) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
}
 
function checkPassword(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length == 4 || txt.length == 5) {
		fieldset.className = "kindagood";
	} else if (txt.length > 5) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}
function checkConf(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	var origPass = document.getElementById('password').value;
	if (txt == origPass && txt.length > 0) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}
 
function checkForLetters(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 0) {
		fieldset.className = "welldone";
	}
	else {
		fieldset.className = "";
	}
}
 
function checkEmail(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}
 
function checkGrade(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	if (whatYouTyped.selectedIndex != 0) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}
 
 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
 
function prepareInputsForHints() {
	
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
  document.getElementById('firstName').focus();
  document.getElementById('firstName').parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
 
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
 
var cashOrCheck = function() {
    var button = document.getElementById('cashCheck');
    if (button.value == 'cash') {
        document.getElementById('checkNum').disabled = true;
    } else {
        document.getElementById('checkNum').disabled = false;
    }
}

Open in new window

0
 
Davejk88Commented:
Putting that code through JSLint returns quite a few errors - try fixing those, and see if it works afterwards. The issue is probably coming from the cashOrCheck function being defined at the end of the file, which will never be reached since the interpreter will stop reading a file when it hits an error.
0
 
bkanewebAuthor Commented:
Thanks for the reply...

while that did find a bunch of errors that I had to fix, that wasn't the cause of it. After becoming desperate and trying everything with the code, I decided to change the name and ID of the radio button. For some reason, I recieved that error when the word 'Check' was in the ID name...perhaps that's a javascript key word or something...

Well, thanks for your help, and i do appreciate it...
0
 
bkanewebAuthor Commented:
Thanks for your help!
0

Featured Post

Technology Partners: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now