functions with variables inside other functions

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

functions with variables inside other functions

Post by Da_Elf »

Ive got a form where i will be using javascript to check if different elements are met. right now ive got everything working however i want to minimise the amount of scripts im using. However i also want to run a whole bunch of scripts when the page loads. so i have a <body onload="chkAll()">
in javascript im calling a bunch of the scripts

Code: Select all

function chkAll(){
script1();
script2();
script3();
other1();
other2();
}
in scripts 1-3 lets say each looks for a specific event on a specific elements id but they all more or less do the same thing.
As is i have it as the first one but it would make sense to put it as the second 1 so its not id specific (this is checking if a checkbox is checked and it will activate a hidden input).

Code: Select all

<input type="checkbox" id="check1" name="check1" value="1" <?php if ($x == 1){echo "checked='checked'";}?> onclick="script1()" />
<input type="hidden" id="hide1" name="check1" value="" disabled="disabled" />

Code: Select all

function current(){
	if (document.getElementById('check1').checked){document.getElementById('hide1').disabled=true;}
	else{document.getElementById('hide1').disabled=false;}
}
function correct(a,b){
	if (document.getElementById(a).checked){document.getElementById(b).disabled=true;}
	else{document.getElementById(b).disabled=false;}
}
what i want to know is how do i put a function with variables like that into another function

Code: Select all

function correct(a,b){
	if (document.getElementById(a).checked){document.getElementById(b).disabled=true;}
	else{document.getElementById(b).disabled=false;}
}
function chkAll(a,b){
correct(a,b); 
other1();
other2();
}/*doesnt work*/
function chkAll(){
correct(a,b); 
other1();
other2();
}/*doesnt work*/
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: functions with variables inside other functions

Post by Christopher »

I am not exactly sure what the question is, but maybe you could do this:

Code: Select all

function correct(a,b){
	if (a === undefined) a = 'check1';
	if (b === undefined) a = 'hide1';
	if (document.getElementById(a).checked) {
		document.getElementById(b).disabled=true;
	} else {
		document.getElementById(b).disabled=false;
	}
}
(#10850)
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: functions with variables inside other functions

Post by Da_Elf »

the function correct(a,b) works fine. what i want to do is put that into another function.
basically there are a lot of those checkboxes that need to be checked so i need setup as a variable

Code: Select all

<input type="checkbox" id="check1" onclick"correct('check1','hide1')" />
<input type="hidden" id="hide1" disabled="disabled" />

<input type="checkbox" id="check2" onclick"correct('check2','hide2')" />
<input type="hidden" id="hide2" disabled="disabled" />

<input type="checkbox" id="check3" onclick"correct('check3','hide3')" />
<input type="hidden" id="hide3" disabled="disabled" />
the function correct(a,b) works fine. however i need that when the page loads ive got quite a few things i want a few javascript functions run so i wrapped them in another function which is executed with
<body onload="chkAll()">
so i need chkAll() to look through my form elements and use the correct(a,b) function on all of the elements
i tried

Code: Select all

function correct(a,b){
        if (document.getElementById(a).checked){document.getElementById(b).disabled=true;}
        else{document.getElementById(b).disabled=false;}
}

function chkAll(){
correct(a,b);
}
it doesnt work
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: functions with variables inside other functions

Post by Christopher »

Da_Elf wrote:so i need chkAll() to look through my form elements and use the correct(a,b) function on all of the elements
i tried

Code: Select all

function correct(a,b){
        if (document.getElementById(a).checked){document.getElementById(b).disabled=true;}
        else{document.getElementById(b).disabled=false;}
}

function chkAll(){
correct(a,b);
}
it doesnt work
Maybe try?

Code: Select all

function chkAll(){
    correct("check1", "hide1");
    correct("check2", "hide2");
    correct("check3", "hide3");
}
(#10850)
Post Reply